pthread issue

G

guddu

Hi,
I have a pthread issue here.
I am trying to compile a small pthread code

#define errexit(code,str) \
fprintf(stderr,"%s: %s\n",(str),strerror(code)); \
exit(1);

/******** this is the thread code */
void *myfun(void * arg)
{

// something something

}


/******** this is the main thread's code */
int main(int argc,char *argv[])
{
int worker;
pthread_t threads[NTHREADS]; // holds thread info
int ids[NTHREADS]; // holds thread args
int errcode; //* holds pthread error
code
int *status; //* holds return code

// create the threads
for (worker=0; worker<NTHREADS; worker++) {
ids[worker]=worker;
if (errcode=pthread_create(&threads[worker],// thread
struct
NULL, //* default thread
attributes
myfun, //* start
routine
&ids[worker])) { //* arg to
routine
errexit(errcode,"pthread_create");
}

}

/*

some other code

*/


return(0);
}



I tried compiling this on a Windows commandline with g++ compiler

C:\C++Files>g++ -lpthread Thread.cpp
E:\DOCUME~1\VISHAL\LOCALS~1\Temp/ccaZD7xf.o:Thread.cpp:(.text+0xb4):
undefined reference to `_imp__pthread_create'
collect2: ld returned 1 exit status

I am not sure why I am getting this error.

any suggestions and advice ??
Guddu
 
E

Ersek, Laszlo

C:\C++Files>g++ -lpthread Thread.cpp
E:\DOCUME~1\VISHAL\LOCALS~1\Temp/ccaZD7xf.o:Thread.cpp:(.text+0xb4):
undefined reference to `_imp__pthread_create'
collect2: ld returned 1 exit status

Put the "-lpthread" operand after the "Thread.cpp" operand.

----v----
-l/library/
-l /library/

Search the library named /library/ when linking. (The second
alternative with the library as a separate argument is only for POSIX
compliance and is not recommended.)

It makes a difference where in the command you write this option; the
linker searches and processes libraries and object files in the order they
are specified. Thus, `foo.o -lz bar.o' searches library `z' after file
foo.o but before bar.o. If bar.o refers to functions in `z', those
functions may not be loaded.
----^----

<http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Link-Options.html>

(And yes, "-l pthread" is an operand, not an option, whatever the gcc docs
might say. <http://www.opengroup.org/onlinepubs/007908775/xcu/c89.html>)

Cheers,
lacos
 
B

Bill Cunningham

If Laszlo's followup didn't answer your question, I suggest asking in
comp.programming.threads.

I was just about to suggest the same thing. I didn't see a topic
concerning standard C.

Bill
 
B

Bill Cunningham

guddu said:
Hi,
I have a pthread issue here.
I am trying to compile a small pthread code
[snip]

I have always been told that UNIX threads are much better than GNU's
pthreads. I've never used threads or read much about them of course linux
comes with posix threads so maybe it would be alright to add pth.

Bill
 
G

guddu

Put the "-lpthread" operand after the "Thread.cpp" operand.

----v----
-l/library/
-l /library/

     Search the library named /library/ when linking. (The second
alternative with the library as a separate argument is only for POSIX
compliance and is not recommended.)
Hi,
That worked.
Putting -lpthread at the end makes the code to compile.

Thanks and Regards,
Vishal
 

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

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top