【发布时间】:2013-10-22 18:17:34
【问题描述】:
当我编译以下程序时(我从64 bit ntohl() in C++? 获得的所有定义的代码似乎很合理):
#include <stdint.h>
#if defined(__linux__)
#include <endian.h> //htobe64,be64toh
#include <arpa/inet.h> //ntohs, ntohl, htonl, htons
#elif defined(__FreeBSD__) || defined(__NetBSD__)
#include <sys/endian.h>
#elif defined(__OpenBSD__)
#include <sys/types.h>
#define be16toh(x) betoh16(x)
#define be32toh(x) betoh32(x)
#define be64toh(x) betoh64(x)
#endif
int main()
{
int64_t i = 0x1212121234343434;
int64_t j = be64toh(i);
return 0;
}
使用以下命令编译时出现链接错误(我正在运行 linux):
gcc -std=c99 endian_test.c -o endian
我收到的错误是:
user@host ~/src/c $ gcc -std=c99 derp.c
endian_test.c: In function ‘main’:
endian_test.c:17:2: warning: implicit declaration of function ‘be64toh’ [-Wimplicit-function-declaration]
int64_t j = be64toh(i);
^
/tmp/ccYonfH4.o: In function `main':
endian_test.c:(.text+0x23): undefined reference to `be64toh'
collect2: error: ld returned 1 exit status
这对我来说表明了两件事,标头本身已包含在内,但并未真正包含使其工作所需的函数/宏,因为这意味着编译器希望稍后它会找到该函数,但无论如何它都会尝试继续进行,但是尝试链接时失败。
但如果我使用以下命令编译(只需删除-std=c99):
gcc endian_test.c -o endian
一切都像黄油一样光滑并且有效。知道为什么会发生这种情况以及我能做些什么来补救吗?对我来说,内核给定的函数(或者我错了?)根据我在编译时使用的标准而改变是没有意义的?
提前致谢!
【问题讨论】:
-
您始终可以自己查看文件以了解它需要什么。
标签: c compilation linker endianness data-conversion