How to resolve compile a static binary which code include a function gethostbyname and if compiled without warning like this:
warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
I compile on ubuntu 12.04 with command:
$ gcc -static lookup.c -o lookup
This is code for lookup.c:
/* lookup.c */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
extern int h_errno;
int main(int argc,char **argv) {
int x, x2;
struct hostent *hp;
for ( x=1; x<argc; ++x ) {
hp = gethostbyname(argv[x]);
if ( !hp ) {
fprintf(stderr,
"%s: host '%s'\n",
hstrerror(h_errno),
argv[x]);
continue;
}
printf("Host %s : \n" ,argv[x]);
printf(" Officially:\t%s\n", hp->h_name);
fputs(" Aliases:\t",stdout);
for ( x2=0; hp->h_aliases[x2]; ++x2 ) {
if ( x2 ) {
fputs(", ",stdout);
}
fputs(hp->h_aliases[x2],stdout);
}
fputc('\n',stdout);
printf(" Type:\t\t%s\n",
hp->h_addrtype == AF_INET
? "AF_INET" : "AF_INET6");
if ( hp->h_addrtype == AF_INET ) {
for ( x2=0; hp->h_addr_list[x2]; ++x2 ) {
printf(" Address:\t%s\n",
inet_ntoa( *(struct in_addr *)
hp->h_addr_list[x2]));
}
}
putchar('\n');
}
return 0;
}
I want to if I check via $ file lookup
will get output like this:
lookup: ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.24, BuildID[sha1]=0x6fcb2684ad8e5e842036936abb50911cdde47c73, not stripped
Not like this:
lookup: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xf9f18671751927bea80de676d207664abfdcf5dc, not stripped
If you commented with suggested I must use without static because different libc every linux I knew it, I hope you do not need to comment. Why do I persist in for static? Because there I need to do to mandatory use static, binary files must be static and not dynamic.
I have more than 2 weeks looking for this but so far have not succeeded.
Thanks for help me to resolve my heavy problem.
libc.so
– Basile Starynkevitch/etc/nsswitch.conf
usesdlopen
forgethostbyname
so needs a dynamically linked program (because the dynamic linker needs to be there). Just compile withgcc -Wall -g -O lookup.c -o lookup
and yourlookup
will be dynamically linked. – Basile Starynkevitch