Windows、Linux、VxWorksでビルドする必要のあるプロジェクトがあります。プロジェクトはLinuxとWindowsで構築されていますが、VxWorks用にクロスコンパイルされています。複数のプラットフォーム間でエンディアンを処理するために、ntoh.hを使用します。Linuxマシンはリトルエンディアンですが、ntohlは私のプログラムでスワップしません。
in.hを直接含むテストプログラムを作成しました。それは適切に交換します。ntoh.hだけを含む別のテストプログラムを作成しました。それは適切に交換します。両方のテストプログラムはlib64/libc.so.6にリンクしています。
ただし、プロジェクトをコンパイルすると、ntohlはスワップしません。gdbの「breakntohl」コマンドを使用してntohlを中断できません。ビルド中に、LITTLE ENDIAN警告(以下を参照)が表示され、「SHOULDNTBEHERE」エラーは表示されません。
助けてください。この問題が発生している理由がわかりません。
以下はntoh.hです:
#ifndef __ntoh__
#define __ntoh__
#include "basic_types.h"
#ifdef WIN32
#include <winsock2.h>
#elif LINUX
#include <netinet/in.h>
//This is here to determine what __BYTE_ORDER is set to in netinet/in.h.
// Not in original code
#if __BYTE_ORDER == __BIG_ENDIAN
#warning BIG ENDIAN BYTE ORDER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#endif
//This is here to determine what __BYTE_ORDER is set to in netinet/in.h.
// Not in original code
#if __BYTE_ORDER == __LITTLE_ENDIAN
#warning YAY LITTLE ENDIAN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#endif
#else
#error SHOULDNT BE HERE //added for debugging purposes
#define ntohl(x) (x)
#define ntohs(x) (x)
#define htonl(x) (x)
#define htons(x) (x)
#endif
#endif // __ntoh__
私のコンパイルコマンドの一部:
g++ -DDAU_PARSER -DNO_MT -DTEST_CLOCK -DLINUX -g -Irelease/include -Irelease/include/Record_Data/ -Irelease/include/Utility -o dauParser DAU_Support_Tools/src/dau_parser.cpp DAU_Support_Tools/src/dau_parser_write_data_to_file.cpp Utility/src/Messaging/Communications/Message.cpp Utility/src/time_type.cpp Utility/src/collectable.cpp Utility/src/clist.cpp Utility/src/clock.cpp Utility/src/test_clock.cpp Utility/src/mutex.cpp Utility/src/ntoh.cpp ...
エラーは次の行によって生成されます。
int deadbeef = 0xDEADBEEF;
printf("TESTING DEADBEEF %x %x\n", deadbeef, ntohl(deadbeef) );
これらの2つのラインからの出力は、同じ出力を生成します。デッドビーフのテストデッドビーフデッドビーフ