C++ 未定义的对 `pthread_mutex_trylock' 的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2009625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:49:47  来源:igfitidea点击:

undefined reference to `pthread_mutex_trylock'

c++pthreads

提问by enzo1959

I have the following test program.

我有以下测试程序。

#include <iostream>
#include <cstdlib>

using namespace std;    
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;

int main(int argc, char *argv[])
{
  int iret;
  iret = pthread_mutex_trylock( & mymutex );
  cout << "Test2 !!! " << endl;
  pthread_mutex_unlock( & mymutex );
  return EXIT_SUCCESS;
}

If I compile it without adding the pthread library I get the error for unresolved error for pthread_mutex_trylock , but only for function pthread_mutex_trylock.

如果我在不添加 pthread 库的情况下编译它,则会收到 pthread_mutex_trylock 的未解决错误的错误,但仅适用于函数 pthread_mutex_trylock。

If I replace pthread_mutex_trylock with pthread_mutex_trylock the program is compiled and runnig well also without the -lpthread* option.

如果我用 pthread_mutex_trylock 替换 pthread_mutex_trylock,则程序也可以在没有 -lpthread* 选项的情况下编译并运行良好。

If I add the -lpthraed option to the compile commands all runs well this run well : $ g++ test2.c -o test2 -lpthreadthis warn unresolved : $ g++ test2.c -o test2

如果我将 -lpthraed 选项添加到编译命令中,则所有运行良好: $ g++ test2.c -o test2 -lpthread此警告未解决: $ g++ test2.c -o test2

Example error output: $ g++ test2.c -o test2 /tmp/ccU1bBdU.o: In function main': test2.c:(.text+0x11): undefined reference topthread_mutex_trylock' collect2: ld returned 1 exit status

示例错误输出:$ g++ test2.c -o test2 /tmp/ccU1bBdU.o: In function main': test2.c:(.text+0x11): undefined reference topthread_mutex_trylock' collect2: ld returns 1 exit status

If I replace the instruction iret = pthread_mutex_trylock( & mymutex );

如果我替换指令iret = pthread_mutex_trylock( & mymutex );

with iret = pthread_mutex_lock( & mymutex );the program compiles and run without error also if didn't add the pthread libarry to the compile command I know that is right to have the unresolved error if I didn't use the -lpthread option , but why I have not the same unresolved error also for other pthread_ function ?

iret = pthread_mutex_lock( & mymutex ); 如果没有将 pthread 库添加到编译命令中,程序编译和运行也不会出错也适用于其他 pthread_ 函数?

I'm using gcc 4.4.2 on fedora 12

我在 Fedora 12 上使用 gcc 4.4.2

$ g++ --version
g++ (GCC) 4.4.2 20091222 (Red Hat 4.4.2-20)
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Do some have some suggestion about the meaning of this unreference only for pthread_mutex_trylock ?

有些人对 pthread_mutex_trylock 的这种未引用的含义有什么建议吗?

thanks for the help, Enzo

感谢您的帮助,恩佐

回答by P Shved

If you use pthread functions you should link your object files with -lpthreadand not worry about whether symbols are included in libc.

如果您使用的并行线程功能,你应该用你的链接对象文件-lpthread,而不用担心符号是否被包括在libc

The rationale behind this is saidto be such: some time ago the stubs in libc were used when application that used threads was run on a system without threading support. On such system, pthread_*functions became linked to libcstubs that returned errors showing that there's no threading functionality. While on "threaded" systems they were linked to pthreadlibrary and worked correctly.

据说这背后的基本原理是这样的:前一段时间,当使用线程的应用程序在没有线程支持的系统上运行时,会使用 libc 中的存根。在这样的系统上,pthread_*函数链接到libc存根,存根返回错误,表明没有线程功能。在“线程”系统上,它们链接到pthread库并正常工作。

Obviously, pthread_mutex_trylockfunction appeared after the policy changed to linking with -lpthread. So there's no stub for it.

显然,pthread_mutex_trylock函数出现在策略改为与 连接后-lpthread。所以没有存根。

回答by Chris Jester-Young

You should perform both the compilation and linkage with the -pthreadoption, to be portable. On some systems, the compilation will have specific flags added (e.g., -D_REENTRANT) with -pthreadspecified.

您应该使用-pthread选项执行编译和链接,以便可移植。在某些系统上,编译将添加(例如,-D_REENTRANT-pthread指定的特定标志。

If you're curious to see what -pthreadwill do to your compile and link flags, run gcc -dumpspecs.

如果您想知道-pthread对编译和链接标志有何影响,请运行gcc -dumpspecs.