Solved

How to compile a socket programming file.

Posted on 2004-03-26
6
349 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

11 Experts available now in Live!

Get 1:1 Help Now