Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to compile a socket programming file.

Posted on 2004-03-26
6
Medium Priority
?
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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 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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

704 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