LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2006, 01:52 AM   #1
hussainahsan
LQ Newbie
 
Registered: Dec 2006
Posts: 3

Rep: Reputation: 0
C pthreads -- gcc compile error


I am trying to add some threads to my distributed FTP code. I'm using the pthreads library. When I compile my code through gcc, here is the error I get:

..
gcc: -lpthread: linker input file unused because linking not done
gcc -O2 -g -lpthread -Wall -W -Wpointer-arith -Wbad-function-cast -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -Winline -D__USE_READLINE__ ruserpass.c -c
gcc: -lpthread: linker input file unused because linking not done
gcc cmds.o cmdtab.o domacro.o ftp.o glob.o main.o ruserpass.o -lreadline -lncurses -o ftp
cmds.o(.text+0x1171): In function `init':
/home/hussain/netkit-ftp-0.17/ftp/cmds.c:827: undefined reference to `pthread_create'
collect2: ld returned 1 exit status
make: *** [ftp] Error 1

Any guidance on why I'm getting this error would be highly appreciated.


UPDATE : I fixed it, basically the issue was I was putting the "-lpthread" in the makefile at the wrong place. It should be before you make an executable!

Last edited by hussainahsan; 12-09-2006 at 02:04 AM.
 
Old 12-09-2006, 07:01 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Only use -l when linking the final executable, not when building the .o files.

Edit: Oops, didn't see the update.
 
Old 12-10-2006, 12:39 AM   #3
hussainahsan
LQ Newbie
 
Registered: Dec 2006
Posts: 3

Original Poster
Rep: Reputation: 0
port used as socket not reusable?

Thanks for the response. I'm having another problem which is that I after I pass in a port to be used as a socket, I am unable to bind() on it again for sometime. Now I thought that by using close or shutdown on the sockfd, I would be able to use that port again. This doesn' seem to be true. Any clues on how to reuse the same port after I've done some data send/receive on it?
 
Old 12-10-2006, 08:55 AM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Try reading
http://beej.us/guide/bgnet/

First, sockets have a tendency to stick around after they are supposed to be dead. For reusing the same port, accept() returns a brand new file descripter for communicating with a specific client.
 
Old 12-11-2006, 12:43 AM   #5
hussainahsan
LQ Newbie
 
Registered: Dec 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for that link. It's quite useful. However, I tried this:

int yes=1;
if (setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}

But after I bind, I still get the "Address already in use" error msg. Now I'm thinking that might have something to do with how I've written my code. I have a main program thread that creates (pthread_create) n threads to listen on n ports. This is not a thread pool so I want the threads to die after completing their job of listening and writing their contents onto file. Now in order make the threads exit, I tried using pthread_join right after I create them. But that doesn't seem to work. Any ideas as to where I should use pthread_join call? Could the threads being still alive after listening and outputting to file be the reason why the my program is unable to bind on the same port(s)?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC errors using Pthreads spaaarky21 Programming 2 07-16-2006 09:36 PM
gcc compile error? wampire Programming 3 05-06-2006 08:05 PM
compile GCC 4.0 error! ayiiq180 Linux - Software 3 04-22-2005 04:04 PM
compile error with gcc 3.3.2 on Mandrake 10 nboul Linux - Software 0 09-26-2004 06:53 PM
gcc-2.3.2 compile error rimo Linux From Scratch 33 07-30-2003 07:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration