Linker error in compilation

S

srini4vasan

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()
 
J

jacob navia

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
 
S

Sheth Raxit

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
 
M

Martin Ambuhl

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top