Solved

How to compile a socket programming file.

Posted on 2004-03-26
6
357 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

772 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