Linux: ntohl does not work correctly

I have a project that should be built on Windows, Linux, and VxWorks. The project is built on Linux and Windows, but compiled for VxWorks. To handle endianness on multiple platforms, it uses ntoh.h. The Linux machine is a little oriented, but ntohl does not change its program.

I wrote a test program that directly includes in.h. It matches accordingly. I wrote another test program that only includes ntoh.h. It matches accordingly. Both test programs reference lib64 / libc.so.6.

However, when I compile my project, ntohl does not swap. I cannot break ntohl using the gdb "break ntohl" command. When creating, I see a LITTLE ENDIAN warning (see below) and don’t see the "FOLLOW HERE" error .

Please, help. I do not understand why this problem occurs.

Below 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 compilation 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) ); 

Exiting these two lines produces the same output. DEADBEEF TEST deadbeef deadbeef

+5
source share
1 answer

Exiting these two lines produces the same output. DEADBEEF TEST deadbeef deadbeef

Something is wrong, but we cannot tell you. You must debug this problem, because you are the only one who can observe it.

:

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

?

  • : .
  • : , - .
    : gcc -dD -E -DLINUX ntoh.cpp , ntohl .

, - , .

#undef ntohl
#define ntohl(x) (x)
+6

All Articles