Solved

How to compile a socket programming file.

Posted on 2004-03-26
6
353 Views
Last Modified: 2010-04-21
Hi
I wanted to compile a very simple program, in which main() calls one socket( ) api to create a socket. But gcc gave me a compile error that socket is not defined.
Here is what I did to compile the test.c
gcc -o test test.c -ldl -lpthread
Undefined                                       first referenced
 symbol                                               in file
socket                                                 /var/tmp//ccOLjlv5.o
ld: fatal: Symbol referencing errors. No output written to test.
collect2: ld returned 1 exit status.

Thanks. I am new to socket programming and want to learn how to create server/client. But this compile error really block me from going forward.

Here is test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>
#include <ctype.h>
#include <dirent.h>
#include <netinet/in.h>

main()
{
      int s;
      if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
            perror("client: socket");
            exit(1);
      }
      return;
}


0
Comment
Question by:chuehw
  • 4
  • 2
6 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10690570
Which version of GCC are you using? You can find this information by running gcc -v
Which Linux distribution are you using?

I can tell you that on SuSE 8.2 with gcc 3.3. this program compiles without any problems.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10690604
BTW: main() should return an integer, and you should not mix exit and return in your main function:

int main()
{
   ...
      return 1;

...
   return 0;
}
0
 

Author Comment

by:chuehw
ID: 10690920
Hi khkremer
I was using gcc 3.3.2.
It is SunOS  sun4u sparc SUNW. Do I need extra library to link with ? Could it be a machine issue ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 50 total points
ID: 10691358
It's been five years since I last programmed with SunOS/Solaris, but I think you have to add the following two libraries to the gcc parameter list:

-lsocket -lnsl
0
 

Author Comment

by:chuehw
ID: 10691430
khkremer,
Thx !!!!!!

It works. Can you tell me what is -lnsl ?  -lsocket is pretty straightfoward.

0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10691901
I don't remember what NSL stands for, but I think it was something about network streams.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now