2

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
  • fatal: Symbol referencing errors.
    – Siddiqui
    Oct 11, 2010 at 11:48
  • "symbol socket is not referenced" is not the error message shown above.
    – jww
    Jul 21, 2018 at 22:29

3 Answers 3

6

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
5
  • @PP, first of all I apologize about my question.. I didn't get your point.. Please explain.
    – Siddiqui
    Oct 11, 2010 at 12:01
  • 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
    Oct 11, 2010 at 12:03
  • @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.
    Oct 11, 2010 at 12:09
  • Thanks PP.. I have got it.. And I will concentrate on my question next time.
    – Siddiqui
    Oct 11, 2010 at 12:20
  • @Arman, if you update your question with the linker error you were getting, I'll remove the downvote and upvote your question.
    – PP.
    Oct 11, 2010 at 12:31
2

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

-lsocket 
2
  • kindly give me some explanation.
    – Siddiqui
    Oct 11, 2010 at 11:49
  • 1
    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
    Oct 11, 2010 at 11:52
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.