Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

socket programming, which library to link with?

394 views
Skip to first unread message

James Yates, IV

unread,
Mar 9, 2003, 11:02:06 AM3/9/03
to
I am trying to compile a file that includes:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>.

Now when i try to compile using:

gcc -Wall -o dns dns.c

It spits it back in my face that the function inet_ntoa() is an unresolved
symbol. So my question is: which library do I link with to get it to link?
What command line do I use?

RedHat Linux 8.0 Kernel 2.4.18-26.8.0


Andreas

unread,
Mar 9, 2003, 11:45:13 AM3/9/03
to

> It spits it back in my face that the function inet_ntoa() is an unresolved
> symbol. So my question is: which library do I link with to get it to link?
> What command line do I use?

Hi James,
please post the whole output of gcc

cu
Andreas

Valentin Nechayev

unread,
Mar 9, 2003, 1:13:18 PM3/9/03
to
>>> James Yates, IV wrote:

JYI> #include <stdio.h>
JYI> #include <stdlib.h>
JYI> #include <errno.h>
JYI> #include <netdb.h>
JYI> #include <sys/types.h>
JYI> #include <sys/socket.h>
JYI> #include <netinet/in.h>
JYI> #include <arpa/inet.h>.
JYI> Now when i try to compile using:
JYI> gcc -Wall -o dns dns.c
JYI> It spits it back in my face that the function inet_ntoa() is an unresolved
JYI> symbol. So my question is: which library do I link with to get it to link?
JYI> What command line do I use?

It is in libc. So standard linking should find and attach it.
As said here, please copy & paste compiler output from it, not your re-telling.

JYI> RedHat Linux 8.0 Kernel 2.4.18-26.8.0

Yes, I've just checked RH 8.0.


-netch-

David Schwartz

unread,
Mar 9, 2003, 3:52:05 PM3/9/03
to

Maybe -lresolve? I'm puzzled why you don't just *check*. The 'nm'
command will list the symbols in a library, and you can 'grep' the
output for 'inet_ntoa'. For example:

nm -o `find /lib -name \*so\*` | grep inet_ntoa | grep -v U

DS

James Yates, IV

unread,
Mar 9, 2003, 4:08:10 PM3/9/03
to
"Andreas" <mikl...@stud.uni-sb.de> wrote in message
news:b4fr3i$56l2$4...@hades.rz.uni-saarland.de...

Sorry about that:

gcc -Wall dns dns.c
> dns.c: In function 'main':
> dns.c:29: warning: implicit declaration of function 'inet_itoa'
> dns.c:29: warning: format argument is not a pointer (arg 2)
> /tmp/cc3yiCfw.o: In function 'main':
> /tmp/cc3yiCfw.o(.text+0x94): undefined reference to 'inet_itoa'
> collect2: ld returned 1 exit status

Maybe I missed a header file?


James Yates, IV

unread,
Mar 9, 2003, 4:12:32 PM3/9/03
to
"Andreas" <mikl...@stud.uni-sb.de> wrote in message
news:b4fr3i$56l2$4...@hades.rz.uni-saarland.de...
>

Here's the code I had:

/*
** dns.c - a hostname lookup demo
*/


#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
struct hostent *h;
if (argc != 2) { // error check the command line
fprintf(stderr,"usage: getip address\n");
exit(1);
}
if ((h=gethostbyname(argv[1])) == NULL) { // get the host info
herror("gethostbyname");
exit(1);
}
printf("Host name : %s\n", h->h_name);
printf("IP Address : %s\n", inet_ntoa(*((struct in_addr *)h->h_addr)));
return 0;
}


j...@invalid.address

unread,
Mar 9, 2003, 10:06:19 PM3/9/03
to

What's "inet_itoa"? The code you posted in another article used
inet_ntoa, but the compiler is seeing inet_itoa for some reason. Are
you sure the code you posted is the same code that's failing here?

James Yates, IV

unread,
Mar 10, 2003, 4:04:12 PM3/10/03
to
<j...@invalid.address> wrote in message
news:m3n0k3z...@invalid.address...

Oops. It was inet_ntoa(). Sorry, guys.


j...@invalid.address

unread,
Mar 10, 2003, 5:54:41 PM3/10/03
to

What was inet_ntoa()? What you actually used in the code, or what gcc
complained about?

I'm just wondering how, if you copied and pasted the output of gcc,
it's complaining about something you didn't have in the code.

If you did have inet_ntoa in the code, how about posting the real
output of gcc?

0 new messages