Solved

How to compile a socket programming file.

Posted on 2004-03-26
6
360 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

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

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
OwnCloud Development and/or customization.. 19 1,128
File system unmounted 3 157
generating rpm using noarch parameter 1 42
yum remove leaves empty directories 1 93
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 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