当前位置: 技术问答>linux和unix
linux平台,线程的问题,编译结果为什么会这样
来源: 互联网 发布时间:2016-04-15
本文导语: sh-3.2$ gcc -o cxqd11-1 cxqd11-1.c /tmp/ccIRAHg4.o: In function `main': cxqd11-1.c:(.text+0x307): undefined reference to `pthread_create' collect2: ld 返回 1 代码如下: #include "apue.h" #include "error.c" #include pthread_t ntid; void printids(const ...
sh-3.2$ gcc -o cxqd11-1 cxqd11-1.c
/tmp/ccIRAHg4.o: In function `main':
cxqd11-1.c:(.text+0x307): undefined reference to `pthread_create'
collect2: ld 返回 1
代码如下:
#include "apue.h"
#include "error.c"
#include
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)
{
err_quit("can't create thread: %sn", strerror(err));
}
printids("main thread: ");
sleep(1);
exit(0);
}
/tmp/ccIRAHg4.o: In function `main':
cxqd11-1.c:(.text+0x307): undefined reference to `pthread_create'
collect2: ld 返回 1
代码如下:
#include "apue.h"
#include "error.c"
#include
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)
{
err_quit("can't create thread: %sn", strerror(err));
}
printids("main thread: ");
sleep(1);
exit(0);
}
|
试一下
在gcc编译命令中加上 -lpthread 试试看
在gcc编译命令中加上 -lpthread 试试看
|
if your environment variable not contain this Lib path,you must use '-L' to link the route
Commonly,the compiler searches '*.so' from the position that was recorded by environment variable.So you can use '-l' to get it.
Here you must appoint the route of the lib file that you want.
|
up
|
Linux下多线程的编译需要在编译命令后加上-lpthread的.
UP~
UP~