当我编译以下程序时(我从C ++中的64位ntohl()获得的所有定义的代码?这似乎很明智):
#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
一切都很顺利,就像黄油一样有效。知道为什么会发生这种情况以及我可以采取什么补救措施吗?对我来说,内核给定的功能(或者我误会为事实?)取决于编译时使用的标准是没有意义的?
提前致谢!
如果没有明确的-std=
选项,调用gcc
相同-std=gnu89
的装置C89 + GNU扩展。GNU扩展将启用宏,这些宏将使您的标头中存在函数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句