Question

I wrote a test program like this:

#include <sys/socket.h>
int main( void ) {
    int  sock = socket(AF_INET, SOCK_DGRAM, 0);
    return 0;
}

And tried to compile it:

$ /tool/sunstudio/bin/cc test.c
Undefined                       first referenced
 symbol                             in file
socket                              test.o
ld: fatal: Symbol referencing errors. No output written to a.out

The output is "symbol socket is not referenced".

Kindly give me the direction so that I can resolve this.

Was it helpful?

Solution

Here's the question.

I wrote a test program like this:

#include <sys/socket.h>
int main( void ) {
    int  sock = socket(AF_INET, SOCK_DGRAM, 0);
    return 0;
}

And tried to compile it so (this is the output that really helps, you have to remember that modern compilers really try their best to help you fix any problems):

$ /tool/sunstudio/bin/cc test.c
Undefined                       first referenced
 symbol                             in file
socket                              test.o
ld: fatal: Symbol referencing errors. No output written to a.out

Now, from the output we can see that the symbol socket is not referenced. So if you type man socket you will get the following from the man page:

SYNOPSIS
     cc [ flag ... ] file ... -lsocket  -lnsl  [ library ... ]

The -l flag indicates that to use this function you need to also link the named library. In this case you are being told to add -lsocket -lnsl to the cc command line as follows:

$ /tool/sunstudio/bin/cc test.c -lsocket -lnsl

OTHER TIPS

you have to link in the socket library, in the command line:

-lsocket 

You need to add at least -lsocket to your link-step, i.e. link against libsocket.so. I don't know how to do that in the SunStudio UI, though - are its projects makefile based?

The man page is usually a good place to look for required libraries; in this case the man page for socket also recommends -lnsl (see the synopsis) so that might be required too but I don't remember it being necessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top