Results 1 to 8 of 8
Here is the code:
Code:
#include <stdio.h>
#include <pthread.h>
void *thread(void *vargp)
{
printf("Hello World!\n");
return NULL;
}
int main()
{
pthread_t tid;
pthread_create(&tid , NULL , thread ,NULL);
pthread_join(tid ...
- 07-26-2007 #1Just Joined!
- Join Date
- Jul 2007
- Location
- Liuyang,Hunan,China
- Posts
- 4
A Link Error about Pthreads
Here is the code:
When I type "gcc m.c" in the shell,it saysCode:#include <stdio.h> #include <pthread.h> void *thread(void *vargp) { printf("Hello World!\n"); return NULL; } int main() { pthread_t tid; pthread_create(&tid , NULL , thread ,NULL); pthread_join(tid , NULL); return 0; }
/home/remir/tmp/ccOoOzic.o: In function `main':
m.c
.text+0x39): undefined reference to `pthread_create'
m.c
.text+0x47): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
How to solve this problem?Thanks!
- 07-26-2007 #2
You have to link it with -lpthread. Including the headers and linking are not the same.
gcc -o whatever whatever.c -lpthread -Wall
I usually do -Wall cause warnings should be on
It's a good habit to get into making warning-free code.
- 07-27-2007 #3Just Joined!
- Join Date
- Jul 2007
- Location
- Liuyang,Hunan,China
- Posts
- 4
Thanks for your advise.
What if I use the routines in <semaphore.h>?
I'm new guy.
- 07-27-2007 #4Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Hi,
To use functions as sem_init, sem_wait, sem_trywait you must include <semaphore.h>.
Regards
- 07-28-2007 #5Just Joined!
- Join Date
- Jul 2007
- Location
- Liuyang,Hunan,China
- Posts
- 4
But it says
undefined reference to 'sem_init'.
I think I may link it with some options,but I don't know.
- 07-28-2007 #6Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Can you copy your code and paste it here within code blocks?
Regards
- 07-28-2007 #7Just Joined!
- Join Date
- Jul 2007
- Location
- Liuyang,Hunan,China
- Posts
- 4
It's simple code.Code:#include <semaphore.h> int main() { sem_t m; sem_init(&m , 0 , 1); return 0; }
But it has a link error:
undefined reference to 'sem_init'.
What does mean of "Regards"?Why your two posts in this thread both have this word?
I'm not English speaker,Thanks!
- 07-28-2007 #8Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Hi,
Use the -lpthread link option.It's simple code.
But it has a link error:
undefined reference to 'sem_init'.
I'm also notWhat does mean of "Regards"?Why your two posts in this thread both have this word?
I'm not Enlish speaker,Thanks!
.
Regards is a synonym for greetings.
Regards


Reply With Quote