【发布时间】:2014-10-06 08:18:23
【问题描述】:
我收到以下错误:
assign4.o: In function `main':
assign4.c:(.text+0x76f): undefined reference to `pthread_broadcast'
assign4.o: In function `threadFunc':
assign4.c:(.text+0x15fd): undefined reference to `pthread_wait'
assign4.c:(.text+0x17f3): undefined reference to `pthread_wait'
collect2: ld returned 1 exit status
编译时:
gcc -pthread assign4.c -o assign4 -lm
或
gcc assign4.c -o assign4 -lm -lpthread
这是对 pthread_broadcast 的调用:
while (killCount < numThreads) {
while (waitCount < numThreads);
waitCount = 0;
pthread_broadcast();
}
对 pthread_wait 的调用:
pthread_wait();
for (i = 0; i < partInfo.groupSize; i++) {
for (j = 0; j < numParts; j++) {
gravitation(bodies[i], parts[i]);
}
}
if (pthread_mutex_lock(&mtx) != 0) {
printf("Error locking mutex.\n");
return -1;
}
waitCount++;
if (pthread_mutex_unlock(&mtx) != 0) {
printf("Error unlocking mutex.\n");
return -1;
}
pthread_wait();
if (pthread_mutex_lock(&mtx) != 0) {
printf("Error locking mutex.\n");
return -1;
}
如您所见,我尝试使用 -pthread 和 -lpthread 进行编译,但无法正常工作。我想这一定是我错过了一些非常明显的东西,但我不知道是什么。
提前谢谢你!
【问题讨论】:
-
我正在使用一个学校服务器,它必须能够在其上进行编译,并且其他人已经成功地在该服务器上编译了 pthread 程序。这是 gcc -v 信息:使用内置规范。目标:x86_64-redhat-linux 配置:
线程模型:posix gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) -
用
-Wall打开编译器警告,GCC会告诉你pthread_wait和pthread_broadcast还没有被声明。
标签: c gcc compilation pthreads