views:

57

answers:

3

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.

+2  A: 

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

-lsocket 
vulkanino
@vulkanino, kindly give me some explanation.
Arman
The Ide you're using (SunStudio) when it compiles uses options you provide in the project settings or makefile. Those options must include all the libraries your program needs; libraries have to be linked (statically or dynamically) to your final executable program. To link socket library, the option is the above -lsocket.
vulkanino
+2  A: 

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.

Rup
+5  A: 

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
PP
@PP, first of all I apologize about my question.. I didn't get your point.. Please explain.
Arman
He means you should have supplied more information: in particular, the error message that you're getting from the linker. He's also suggesting you provide a complete compilable example for us to reproduce with, but I'm not sure if that's always necessary.
Rup
@Arman, apologies, I do come across harsh sometimes. It was, specifically, the linker error I was looking for. You were lucky in that your problem is a common one and most of us can determine the solution straight away. But 9 times out of 10 this is not the case - and it is the specific error returned by the program in question that will help others solve your problem.
PP
Thanks PP.. I have got it.. And I will concentrate on my question next time.
Arman
@Arman, if you update your question with the linker error you were getting, I'll remove the downvote and upvote your question.
PP
@PP, I have update my question.
Arman