Linux中,对‘pthread_create’未定义的引用 bug 问题解决办法:
由于pthread库不是Linux系统默认的库,连接时需要使用库libpthread.a,所以在使用pthread_create创建线程时,在编译中要加-lpthread参数:
gcc -o pthread -lpthread pthread.c
如果还不对,很有可能是-lpthread
放置的位置不对:
gcc pthread.c -lpthread -o pthread
如果使用 vscode,可以将 tasks.json 中 args也改成如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": ["-g", "${file}", "-std=c++11", "-lpthread", "-o", "${fileBasenameNoExtension}.out"]
}
]
}