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.
views:
61answers:
2The standard header is:
#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.
EDIT: 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);
}
and htonl_include.c:
// portable
#include <arpa/inet.h>
#include <stdio.h>
int main()
{
int x = 1;
x = htonl(x);
printf("%d\n", x);
}
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.
If you don't want to include anything network-related, it's perfectly valid to declare htonl
yourself. Just #include <stdint.h>
to get uint32_t
and use the following prototype:
uint32_t htonl(uint32_t);
Reference: POSIX: http://www.opengroup.org/onlinepubs/9699919799/functions/htonl.html
You can also just implement your own by testing byte order (at compiletime) using unions. This doesn't require any odd ENDIAN macros or #ifdefs.