I have a simple code in the file /tmp/prog.c (using a copied line from the open source rdma-core package):
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <endian.h>
static inline __attribute__((deprecated)) uint64_t ntohll(uint64_t x) { return be64toh(x); }
int main() {
printf("Hellon");
return 0;
}
When compiling with the line gcc /tmp/prog.c -o /tmp/prog -Wall
I get no errors.
When compiling with the line gcc /tmp/prog.c -o /tmp/prog -Wall -std=c11
I get the error:
/tmp/prog.c: In function ‘ntohll’:
/tmp/prog.c:14:80: error: implicit declaration of function ‘be64toh’ [-Wimplicit-function-declaration]
14 | static inline attribute((deprecated)) uint64_t ntohll(uint64_t x) { return be64toh(x); }
When compiling with the line gcc /tmp/prog.c -o /tmp/prog -Wall -std=c11 -D_DEFAULT_SOURCE
I once again get no errors.
I understand that this has to do with the following snippet in endian.h:
#if defined __USE_MISC && !defined __ASSEMBLER__
/* Conversion interfaces. */
# include <bits/byteswap.h>
# include <bits/uintn-identity.h>
# if __BYTE_ORDER == __LITTLE_ENDIAN
# define htobe16(x) __bswap_16 (x)
# define htole16(x) __uint16_identity (x)
# define be16toh(x) __bswap_16 (x)
# define le16toh(x) __uint16_identity (x)
# define htobe32(x) __bswap_32 (x)
# define htole32(x) __uint32_identity (x)
# define be32toh(x) __bswap_32 (x)
# define le32toh(x) __uint32_identity (x)
# define htobe64(x) __bswap_64 (x)
# define htole64(x) __uint64_identity (x)
# define be64toh(x) __bswap_64 (x)
# define le64toh(x) __uint64_identity (x)
# else
# define htobe16(x) __uint16_identity (x)
# define htole16(x) __bswap_16 (x)
# define be16toh(x) __uint16_identity (x)
# define le16toh(x) __bswap_16 (x)
# define htobe32(x) __uint32_identity (x)
# define htole32(x) __bswap_32 (x)
# define be32toh(x) __uint32_identity (x)
# define le32toh(x) __bswap_32 (x)
# define htobe64(x) __uint64_identity (x)
# define htole64(x) __bswap_64 (x)
# define be64toh(x) __uint64_identity (x)
# define le64toh(x) __bswap_64 (x)
# endif
#endif
because when I comment out if defined __USE_MISC
I get no errors again with the initial compilation script.
I need to understand what the purpose of __USE_MISC is and why _DEFAULT_SOURCE includes it and why does c11 not.
Also what are the implications of defining _DEFAULT_SOURCE versus not defining anything?
Any help is much appreciated!!
P.S. I understand the line above is deprecated but it’s in an opensource package that I use. Thanks!
5