C11中的Pthread_create函数

时间:2016-02-10 13:31:44

标签: c pthreads

我尝试使用pthread创建Pthread_create

#include <pthread.h>

void *worker(void *arg) {
    return NULL;
}

int main(int argc, char *argv[])
{
    pthread_t p1, p2;

    Pthread_create(&p1, NULL, worker, NULL);
    Pthread_create(&p2, NULL, worker, NULL);

    return 0;
}
  

clang -Wall -std = c11 -pthread thread.c -o thread

架构x86_64的未定义符号:"_Pthread_create"

thread.c:13:2: warning: implicit declaration of function 'Pthread_create' is
      invalid in C99 [-Wimplicit-function-declaration]
        Pthread_create(&p1, NULL, worker, NULL);
        ^
1 warning generated.
Undefined symbols for architecture x86_64:
  "_Pthread_create", referenced from:
      _main in thread-3223a1.o
ld: symbol(s) not found for architecture x86_64
clang-3.8: error: linker command failed with exit code 1 (use -v to see invocation)

改为-std=c99

后没有任何变化

我的代码出了什么问题?

  

clang --version

clang version 3.8.0 (http://llvm.org/git/clang.git 68170291648f0112957a8b3d6912a1a1fed81965) (http://llvm.org/git/llvm.git 92ca4a0cd38e8f17e62ecf6e93a44c8ecf098b12)
Target: x86_64-apple-darwin15.3.0
Thread model: posix

PS:我不想改用C ++ ,因为我正在练习一本操作系统(UNIX C环境)。

1 个答案:

答案 0 :(得分:2)

错误表明链接器无法找到符号Pthread_create。这是pthread_create还是Pthread_create?您还需要将库链接到&#34; -lpthread&#34;。