473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Linker error in compilation

Hi,
I wrote a small socket program in C++ using unix machine.
Compiler CC.
It is asking some library file or linker file
Could you please send the library files or linker files for following
program.

error:
$ CC server_socket.cpp -lsocket -lm -lc -lrt
Undefined first referenced
symbol in file
int unlink(const char*) server_socket.o
int close(int) server_socket.o
---------------------------------------------------------------------------*-----
$CC client_socket.cpp -lsocket -lm -lc -lrt
Undefined first referenced
symbol in file
int close(int) client_socket.o
---------------------------------------------------------------------------*------
Program:
-----------------
server: server_socket.cpp
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#define SOCK_PATH "echo_socket"
int main(void)
{
int s, s2, t, len;
struct sockaddr_un local, remote;
char str[100];
int unlink(const char *path);
int close(int);
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
local.sun_family = AF_UNIX;
strcpy(local.sun_path, SOCK_PATH);
unlink(local.sun_path);
len = strlen(local.sun_path) + sizeof(local.sun_family);
if (bind(s, (struct sockaddr *)&local, len) == -1) {
perror("bind");
exit(1);
}
if (listen(s, 5) == -1) {
perror("listen");
exit(1);
}
for(;;) {
int done, n;
printf("Waiting for a connection...\n");
t = sizeof(remote);
if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) ==
-1) {
perror("accept");
exit(1);
}
printf("Connected.\n");
done = 0;
do {
n = recv(s2, str, 100, 0);
if (n <= 0) {
if (n < 0) perror("recv");
done = 1;
}
if (!done)
if (send(s2, str, n, 0) < 0) {
perror("send");
done = 1;
}
} while (!done);
close(s2);
}
return 0;
}
---------------------------------------------------------------------------*------------------------------------------------
client: client_socket.cpp
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#define SOCK_PATH "echo_socket"
int main(void)
{
int s, t, len;
struct sockaddr_un remote;
char str[100];
int close(int a);
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
printf("Trying to connect...\n");
remote.sun_family = AF_UNIX;
strcpy(remote.sun_path, SOCK_PATH);
len = strlen(remote.sun_path) + sizeof(remote.sun_family);
if (connect(s, (struct sockaddr *)&remote, len) == -1) {
perror("connect");
exit(1);
}
printf("Connected.\n");
while(printf(""), fgets(str, 100, stdin), !feof(stdin)) {
if (send(s, str, strlen(str), 0) == -1) {
perror("send");
exit(1);
}
if ((t=recv(s, str, 100, 0)) 0) {
str[t] = '\0';
printf("echo%s", str);
} else {
if (t < 0) perror("recv");
else printf("Server closed connection\n");
exit(1);
}
}
close(s);
return 0;
}
---------------------------------------------------------------------------*----
Please send me the linker file for close() and unlink()

Jul 6 '07 #1
4 2392
sr*********@gmail.com wrote:
Hi,
I wrote a small socket program in C++ using unix machine.
Compiler CC.
It is asking some library file or linker file
Could you please send the library files or linker files for following
program.

error:
$ CC server_socket.cpp -lsocket -lm -lc -lrt
Undefined first referenced
symbol in file
int unlink(const char*) server_socket.o
int close(int) server_socket.o
---------------------------------------------------------------------------*-----
$CC client_socket.cpp -lsocket -lm -lc -lrt
Undefined first referenced
symbol in file
int close(int) client_socket.o
---------------------------------------------------------------------------*------
Program:
-----------------
server: server_socket.cpp
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#define SOCK_PATH "echo_socket"
int main(void)
{
int s, s2, t, len;
struct sockaddr_un local, remote;
char str[100];
int unlink(const char *path);
int close(int);

You should never do this. Your prototype is probably wrong for the
implementation you are using. Always use the correct header files
for getting the prototypes of system functions.

Eliminate this prototypes and recompile.

jacob
Jul 6 '07 #2
On Jul 6, 2:24 pm, srini4va...@gmail.com wrote:
Hi,
I wrote a small socket program in C++ using unix
machine.
Compiler CC.
CC ? C++ ?
It is asking some library file or linker file
Could you please send the library files or linker files for following
program.

error:

$ CC server_socket.cpp -lsocket -lm -lc -lrt

Undefined first referenced
symbol in file
int unlink(const char*) server_socket.o
int close(int) server_socket.o

---------------------------------------------------------------------------**-----

$CC client_socket.cpp -lsocket -lm -lc -lrt
Undefined first referenced
symbol in file
int close(int) client_socket.o

---------------------------------------------------------------------------**------

Program:
-----------------
server: server_socket.cpp
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#define SOCK_PATH "echo_socket"

int main(void)
{
int s, s2, t, len;
struct sockaddr_un local, remote;
char str[100];
int unlink(const char *path);
int close(int);

if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}

local.sun_family = AF_UNIX;
strcpy(local.sun_path, SOCK_PATH);
unlink(local.sun_path);
len = strlen(local.sun_path) + sizeof(local.sun_family);
if (bind(s, (struct sockaddr *)&local, len) == -1) {
perror("bind");
exit(1);
}

if (listen(s, 5) == -1) {
perror("listen");
exit(1);
}

for(;;) {
int done, n;
printf("Waiting for a connection...\n");
t = sizeof(remote);
if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) ==
-1) {
perror("accept");
exit(1);
}

printf("Connected.\n");

done = 0;
do {
n = recv(s2, str, 100, 0);
if (n <= 0) {
if (n < 0) perror("recv");
done = 1;
}

if (!done)
if (send(s2, str, n, 0) < 0) {
perror("send");
done = 1;
}
} while (!done);

close(s2);
}

return 0;
}
---------------------------------------------------------------------------**------------------------------------------------
client: client_socket.cpp

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#define SOCK_PATH "echo_socket"

int main(void)
{
int s, t, len;
struct sockaddr_un remote;
char str[100];
int close(int a);

if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}

printf("Trying to connect...\n");

remote.sun_family = AF_UNIX;
strcpy(remote.sun_path, SOCK_PATH);
len = strlen(remote.sun_path) + sizeof(remote.sun_family);
if (connect(s, (struct sockaddr *)&remote, len) == -1) {
perror("connect");
exit(1);
}

printf("Connected.\n");

while(printf(""), fgets(str, 100, stdin), !feof(stdin)) {
if (send(s, str, strlen(str), 0) == -1) {
perror("send");
exit(1);
}

if ((t=recv(s, str, 100, 0)) 0) {
str[t] = '\0';
printf("echo%s", str);
} else {
if (t < 0) perror("recv");
else printf("Server closed connection\n");
exit(1);
}
}

close(s);

return 0;
}

---------------------------------------------------------------------------**----

Please send me the linker file for close() and unlink()
for which OS ? on my solaris 5.8,linux, compilation using gcc works
without any Warning/Error. < i didn't look what your program is
doing , just try to compile the same>

Is any thing related to C ?
-Raxit

Jul 6 '07 #3
sr*********@gmail.com wrote:
Hi,
I wrote a small socket program in C++ using unix machine.
If this were a C++ problem (it isn't), you should ask in a C++
newsgroup. C++ is not C.
Compiler CC.
It is asking some library file or linker file
It is possible that you are missing a library, but unlikely. Your code
is missing a needed but non-standard (in C _and_ C++) header.
Could you please send the library files or linker files for following
program.
No. This newsgroup is not the vendor of your implementation, not is it
a sources.wanted newsgroup.
error:
$ CC server_socket.cpp -lsocket -lm -lc -lrt
Undefined first referenced
symbol in file
int unlink(const char*) server_socket.o
int close(int) server_socket.o
---------------------------------------------------------------------------Â*-----
$CC client_socket.cpp -lsocket -lm -lc -lrt
Undefined first referenced
symbol in file
int close(int) client_socket.o
The functions unlink() and close() are not standard C functions. They
are POSIX functions, for which a UNIX newsgroup might be appropriate but
not a C (or C++) newsgroup. In UNIX environments these functions are
declared in <unistd.h>, not a standard C header. Neither your
socket.cpp nor your client_socket.cpp programs include this header.

Your failure to #include <unistd.h>, your imagining that the resulting
is something that can be fixed with a "library file or linker file",
your thinking that a C newsgroup is an appropriate place to ask, as well
as several features of your code which are at best non-portable and
unidiomatic, all suggest that you are not ready to do what you are
trying to do. Attempting to do this level of programming when you have
not learned the basics of C or of the UNIX libraries is foolish. The
potential for harm is great.
Jul 6 '07 #4
Sheth Raxit wrote:
On Jul 6, 2:24 pm, srini4va...@gmail.com wrote:
Hi,
I wrote a small socket program in C++ using unix
machine.
Is any thing related to C ?

Please don't quote an entire large post just for a short reply like
that.


Brian
Jul 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Baloff | last post by:
Hello I have this linker error which makes me think that the definition file is not being seen by the linker, this code is taken from "Thinking in C++, 2nd Edition, Volume 1, Annotated Solutions...
9
by: Steven | last post by:
I am compiling with GCC 3.2, and it works well enough. However, sometimes when I try to link libraries, the linker throws "NOTAD.o(.eh_frame+0x11):NOTAD.C: undefined reference to...
0
by: Pavel | last post by:
Hi, I have c++ class library in which I use fopen/fclose from <stdio.h>. Compilation is OK, but the linker returns the following error: lib_jpeg error LNK2001: unresolved external symbol "struct...
0
by: Tom McDermott | last post by:
I am having linker errors : error LNK2022: metadata operation failed (80131188) : Inconsistent field declarations in duplicated types This code linked sucessfully in C++.NET 2002, but does...
6
by: Igor Kravtchenko | last post by:
Hi, I'm having a pretty idiot problem under VC++ .NET but cannot solve it. I have an .exe A and a dll B. A uses some methods of B (so A depends of B). B exports its methods using...
1
by: developer | last post by:
Hi All I have made a .NET project. the files included are borland c++ files that i am migrate to VC++ .NET I am using Microsoft Visual C++ .NET 2003. the compilation goes through properly,...
0
by: Ehsan68 | last post by:
Hello my problem is "Linker Error: Undefined symbol _cga_driver_far in module maze.c " then Go to Options =>Linker =>Libraries This will display a box showing Container Class Turbo...
1
by: Deepath G | last post by:
This is deepath.. I am getting some linker error when i am trying to connect Websphere MQ using Borland C++ Builder 2006 using imqi.hpp on windows. Error Message ----------------------- ...
2
by: Markus Dehmann | last post by:
What to do if an external library is header-files-only (but you have to use it), and you get lots of linker errors? You will necessarily get linker errors "multiple definition of ..." if you try...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.