简体   繁体   English

Linux:ntohl无法正常工作

[英]Linux: ntohl does not work correctly

I have a project that needs to build on Windows, Linux, and VxWorks. 我有一个需要在Windows,Linux和VxWorks上构建的项目。 The project is built on Linux and Windows but cross compiled for VxWorks. 该项目建立在Linux和Windows上,但是为VxWorks交叉编译。 To handle endianness across multiple platforms, it uses ntoh.h. 要处理跨多个平台的字节序,它使用ntoh.h. The Linux machine is little endian but ntohl doesn't swap in my program. Linux机器是小端,但是ntohl不会交换我的程序。

I wrote a test program that directly includes in.h. 我写了一个直接包含in.h的测试程序。 That swaps appropriately. 交换得恰到好处。 I wrote another test program that just includes the ntoh.h. 我写了另一个包含ntoh.h的测试程序。 That swaps appropriately. 交换得恰到好处。 Both test programs link to lib64/libc.so.6. 两个测试程序都链接到lib64 / libc.so.6。

However, when I compile my project, ntohl doesn't swap. 但是,当我编译我的项目时,ntohl不会交换。 I can't break on ntohl using gdb "break ntohl" command. 我无法使用gdb“break ntohl”命令打破ntohl。 When building, I see LITTLE ENDIAN warning (see below) and do not see the "SHOULDNT BE HERE" error. 在构建时,我看到LITTLE ENDIAN警告(见下文)并且没有看到“SHOULDNT BE HERE”错误。

Please help. 请帮忙。 I don't understand why this problem is occurring. 我不明白为什么会出现这个问题。

Below is ntoh.h: 下面是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__

Part of my compile command: 我的编译命令的一部分:

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 ... 

The error is generated by the following lines: 错误由以下行生成:

int deadbeef = 0xDEADBEEF; 
printf("TESTING DEADBEEF %x %x\n", deadbeef, ntohl(deadbeef) ); 

The output from those two lines produce same output. 这两行的输出产生相同的输出。 TESTING DEADBEEF deadbeef deadbeef 测试DEADBEEF deadbeef deadbeef

The output from those two lines produce same output. 这两行的输出产生相同的输出。 TESTING DEADBEEF deadbeef deadbeef 测试DEADBEEF deadbeef deadbeef

Well, something is wrong, but we can't tell you what. 嗯,有些事情错的,但我们无法告诉你什么。 You have to debug this problem, as you are the only one who can observe it. 必须调试此问题,因为您是唯一可以观察它的人。

Start with the simplest possible example: 从最简单的示例开始:

cat t.c; gcc t.c && ./a.out
#include <netinet/in.h>
#include <stdio.h>

int main() {
  int deadbeef = 0xDEADBEEF; 
  printf("TESTING DEADBEEF %x %x\n", deadbeef, ntohl(deadbeef));
  return 0;
}


TESTING DEADBEEF deadbeef efbeadde

Did this produce expected result? 这是否产生预期结果?

  • No: your toolchain and headers are busted. 不:您的工具链和标题已被破坏。
  • Yes: your toolchain is ok, but your actual code does something different from the example. 是的:您的工具链没问题,但您的实际代码与示例有所不同。
    Run your code through preprocessor: gcc -dD -E -DLINUX ntoh.cpp , and look at what the ntohl macro expands to, and where it's coming from. 通过预处理器运行代码: gcc -dD -E -DLINUX ntoh.cpp ,看看ntohl宏扩展到什么,以及它来自何处。

My guess is that you have something stupid in one of your headers, eg 我的猜测是,你有一些愚蠢的事在你的头,例如一个

#undef ntohl
#define ntohl(x) (x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM