我应该#include使用'htonl'? [英] What should I #include to use 'htonl'?
问题描述
我想在我的ruby c扩展中使用 htonl
函数,但不想使用随附的任何其他互联网内容。什么是最简约的文件 #include
仍然可移植?查看计算机上的头文件,我可以看到 machine / endian.h
或 sys / _endian.h
会让我使用它们,虽然我不确定这是不是一个好主意。
I want to use the htonl
function in my ruby c extension, but don't want to use any of the other internet stuff that comes with it. What would be the most minimalistic file to #include
that is still portable? Looking through the header files on my computer, I can see that either machine/endian.h
or sys/_endian.h
would let me use them, although I am not sure if that is a good idea.
推荐答案
标准标题是:
#include <arpa/inet.h>
您不必担心该标头中定义的其他内容。它不会影响您编译的代码,并且只会对编译时间产生轻微影响。
You don't have to worry about the other stuff defined in that header. It won't affect your compiled code, and should have only a minor effect on compilation time.
编辑:您可以对此进行测试。创建两个文件,htonl_manual.c
You can test this. Create two files, htonl_manual.c
// non-portable, minimalistic header
#include <byteswap.h>
#include <stdio.h>
int main()
{
int x = 1;
x = __bswap_32(x);
printf("%d\n", x);
}
和htonl_include.c:
and htonl_include.c:
// portable
#include <arpa/inet.h>
#include <stdio.h>
int main()
{
int x = 1;
x = htonl(x);
printf("%d\n", x);
}
将它们组装在-O1,然后取差价:
Assemble them at -O1, then take the difference:
gcc htonl_manual.c -o htonl_manual.s -S -O1
gcc htonl_include.c -o htonl_include.s -S -O1
diff htonl_include.s htonl_manual.s
对我而言,唯一的区别是文件名。
For me, the only difference is the filename.
这篇关于我应该#include使用'htonl'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!