Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pthread error

Reply
Thread Tools

pthread error

 
 
asit
Guest
Posts: n/a
 
      08-11-2009
This is the code snippet with main function..

#include <stdio.h>
#include <pthread.h>

pthread_t ntid;

void printids(const char *s) {
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x) \n", s, (unsigned int)pid, (unsigned
int)tid, (unsigned int)tid);
}

void * thr_fn(void *arg) {
printids("new thread : ");
return ((void*)0);
}

int main() {
int err;
err = pthread_create(&ntid, NULL, thr_fn, NULL);
if(err != 0)
printf("can't create thread \n");
printids("main thread : ");
sleep(1);
return 0;
}

When I tried to compile this, it showed the following error ???

asit@asit-desktop:~/cpp$ gedit pthread1.c
asit@asit-desktop:~/cpp$ gcc -o pthread1 pthread1.c
/tmp/ccgO2hyj.o: In function `main':
pthread1.c.text+0x85): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
 
Reply With Quote
 
 
 
 
Hamiral
Guest
Posts: n/a
 
      08-11-2009
asit a écrit :
> asit@asit-desktop:~/cpp$ gedit pthread1.c
> asit@asit-desktop:~/cpp$ gcc -o pthread1 pthread1.c
> /tmp/ccgO2hyj.o: In function `main':
> pthread1.c.text+0x85): undefined reference to `pthread_create'
> collect2: ld returned 1 exit status


You forgot to link with pthread...

Try compiling with
gcc -o pthread1 pthread1.c -lpthread
 
Reply With Quote
 
 
 
 
Mikko Rauhala
Guest
Posts: n/a
 
      08-11-2009
(Off-topic for comp.lang.c, moving to comp.unix.programmer where
this seems more suited.)

On Tue, 11 Aug 2009 17:38:26 +0200, Hamiral <(E-Mail Removed)> wrote:
>>> asit@asit-desktop:~/cpp$ gcc -o pthread1 pthread1.c

>> /tmp/ccgO2hyj.o: In function `main':
>> pthread1.c.text+0x85): undefined reference to `pthread_create'
>> collect2: ld returned 1 exit status

>
> You forgot to link with pthread...
>
> Try compiling with
> gcc -o pthread1 pthread1.c -lpthread


With gcc, you should rather use -pthread which should set both preprocessor
and linker flags appropriately for using pthreads on the current platform.

Just -lpthread may of course work for some use cases.

--
Mikko Rauhala <(E-Mail Removed)> - http://www.iki.fi/mjr/blog/
The Finnish Pirate Party - http://piraattipuolue.fi/
World Transhumanist Association - http://transhumanism.org/
Singularity Institute - http://singinst.org/
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-11-2009
asit <(E-Mail Removed)> writes:
> This is the code snippet with main function..
>
> #include <stdio.h>
> #include <pthread.h>

[snip]
> When I tried to compile this, it showed the following error ???
>
> asit@asit-desktop:~/cpp$ gedit pthread1.c
> asit@asit-desktop:~/cpp$ gcc -o pthread1 pthread1.c
> /tmp/ccgO2hyj.o: In function `main':
> pthread1.c.text+0x85): undefined reference to `pthread_create'
> collect2: ld returned 1 exit status


Note that <pthread.h> is not a standard C header.

You need to tell the compiler (actually the linker) where to find the
library that implements pthreads. The way to do this can vary from
system to system and from compiler to compiler.

(For gcc, it's probably either "-pthread" or "-lpthread". Consult
your system's documentation, perhaps "man pthreads", to be sure.)

If you have more questions about thread programming, try
comp.programming.threads.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
asit
Guest
Posts: n/a
 
      08-11-2009
Thank you...
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pthread sleep cycle problem lokb C++ 7 07-14-2004 09:04 PM
gcc -pthread option for Linux Avin C++ 2 05-08-2004 09:53 AM
PThread Problem (Urgent Help Needed) R Mar Java 0 01-03-2004 12:33 PM
Building Python 2.3 on HP-UX 10.20 - pthread failure Dan Cescato Python 9 10-06-2003 01:40 AM
Strange Pthread Behavior On IRIX Keith C Programming 1 06-26-2003 02:08 PM



Advertisments