我收到以下错误:

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_wait()的函数,可能是pthread_cond_wait()。这就是为什么您收到链接器错误的原因。当链接器无法找到函数定义时,它将给出undefined reference to错误。所以你越来越

assign4.c:(.text+0x15fd): undefined reference to `pthread_wait'
assign4.c:(.text+0x17f3): undefined reference to `pthread_wait'


我认为对于pthread_broadcast()也是同样的错误,应该是pthread_cond_broadcast()

关于c - 对使用-pthread和-lpthread编译的pthread_wait的 undefined reference ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25278254/

10-11 18:25