我有一個需要在Windows,Linux和VxWorks上構建的項目。該項目建立在Linux和Windows上,但是針對VxWorks進行交叉編譯。爲了處理跨多個平臺的字節序,它使用ntoh.h. Linux機器是小端,但ntohl不交換我的程序。Linux:ntohl無法正常工作
我寫了一個測試程序,直接包含in.h.這適當地交換。 我寫了另一個測試程序,只包含ntoh.h.這適當地交換。兩個測試程序鏈接到lib64/libc.so.6。
但是,當我編譯我的項目時,ntohl不交換。我無法使用gdb「break ntohl」命令在ntohl上斷開連接。在建築時,我看到LITTLE ENDIAN警告(請參閱下文),看不到「SHOULDNT BE HERE」錯誤。
請幫忙。我不明白爲什麼會出現這個問題。
下面是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));
從這些輸出兩行產生相同的輸出。 TESTING DEADBEEF deadbeef deadbeef
請添加實際無法正常工作的代碼。 – wRAR 2013-02-25 21:55:11
從你的編輯中,'printf(「%x」,0xdeadbeef)'產生'efbeadde'怎麼可能?您可能想要發佈一個顯示真實代碼的最簡單示例。 – 2013-02-25 22:18:55
@奧斯汀菲利普斯:對不起,你是對的。輸出描述是固定的。 – Kat 2013-02-25 22:23:19