C++ 在c+中使用pthread+;

C++ 在c+中使用pthread+;,c++,linker,pthreads,linker-errors,C++,Linker,Pthreads,Linker Errors,我在*.cc文件中使用pthread.h。当我尝试使用pthread_exit(0)时或pthread_-join(mythrds[yy],NULL)上面写着: .cc:(.text+0x3e): undefined reference to `pthread_exit' 当在带有gcc的*.c文件中编译非常相似的代码时,它工作得非常完美。如何在C++中使用pTrk?(我还添加了-lpthread) 旗帜: g++ -lpthread -Wall -static -W -O9 -funroll

我在
*.cc
文件中使用
pthread.h
。当我尝试使用
pthread_exit(0)时
pthread_-join(mythrds[yy],NULL)上面写着:

.cc:(.text+0x3e): undefined reference to `pthread_exit'
当在带有gcc的
*.c
文件中编译非常相似的代码时,它工作得非常完美。如何在C++中使用pTrk?(我还添加了-lpthread)

旗帜:

g++ -lpthread -Wall -static -W -O9 -funroll-all-loops -finline -ffast-math

pthread头文件是否在函数原型周围有
extern“C”{…}
?这是链接器无法在C++中链接的常见情况。

它之所以发生是因为C++通常命名为Mangle,以便它可以将参数细节编码成符号(允许多态)。例如,功能:

void x(int);
void x(void);
void x(char,int,float,double);
它们都获得不同的链接器符号

如果头文件没有
extern“C”{…}
,您可能需要自己执行以下操作:

extern "C" {
    #include <pthread.h>
}
extern“C”{
#包括
}

希望这能起作用。

您可以尝试在g++中使用-pthread选项

   -pthread
       Adds support for multithreading with the pthreads library.  This
       option sets flags for both the preprocessor and linker.

这很有帮助。我原以为-lpthread也在做同样的事情,但事实并非如此。您可以使用“gcc-dumpspecs”并查找以“%$pthread”开头的内容,来了解-pthread在您的平台上的作用,-lpthread”选项只包括pthread库。在您的平台上,这可能不足以获得pthreads support.thnk,但问题并不是这样。
   -pthread
       Adds support for multithreading with the pthreads library.  This
       option sets flags for both the preprocessor and linker.