【问题标题】:Undefined reference to pthread_create in LinuxLinux 中对 pthread_create 的未定义引用
【发布时间】:2022-01-03 14:44:43
【问题描述】:

我从https://computing.llnl.gov/tutorials/pthreads/ 处从网上获取了以下演示

#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS     5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0; t<NUM_THREADS; t++){
      printf("In main: creating thread %ld\n", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

但是当我在我的机器上编译它(运行 Ubuntu Linux 9.04)我得到以下错误:

corey@ubuntu:~/demo$ gcc -o term term.c
term.c: In function ‘main’:
term.c:23: warning: incompatible implicit declaration of built-in function ‘exit’
/tmp/cc8BMzwx.o: In function `main':
term.c:(.text+0x82): undefined reference to `pthread_create'
collect2: ld returned 1 exit status

这对我来说没有任何意义,因为标题包含pthread.h,它应该具有pthread_create 函数。有什么想法吗?

【问题讨论】:

  • 另外:根据平台,您可能需要 (a) 不同的线程编译器,(b) 不同的线程库(即-lc_r),(c) -thread 或 @ 987654328@ 或其他,代替或补充-lpthread
  • 略高于该示例,您将看到正确的编译器命令表,无论是 GCC、IBM 等。“Employed Russian”是正确的。
  • 您能否取消标记我的答案,以便我删除它(并标记实际正确的答案,即投票最高的答案)?
  • 编译时需要-lpthread
  • 解决方案LDFLAGS= -pthread -lpthread

标签: c linux multithreading pthreads


【解决方案1】:

对于 Linux,正确的命令是:

gcc -pthread -o term term.c

一般来说,库应该遵循命令行上的源和对象,-lpthread 不是一个“选项”,它是一个库规范。在只安装了libpthread.a 的系统上,

gcc -lpthread ...

将无法链接。

【讨论】:

  • +1 这个解决方案有效......其他人没有。此外,“图书馆应遵循来源和对象”的建议是很好的建议——引用或进一步解释会很好。
  • @sholsapp 这里是解释:webpages.charter.net/ppluzhnikov/linker.html
  • 这对我来说仍然是错误的,直到我将 -lpthread 放在命令的最后。 gcc term.c -lpthread
  • 对于使用 CODEBLOCKS 的任何人:将 -pthread 添加到项目构建选项 -> 链接器设置 -> 其他链接器选项。
  • “在只安装了 libpthread.a 的系统上”......还是libpthread.so,对吧?
【解决方案2】:

对于 Linux,正确的命令是:

gcc -o term term.c -lpthread
  1. 你必须在编译命令之后加上-lpthread,这个命令会告诉编译器执行带有pthread.h库的程序。
  2. gcc -l 链接库文件。链接 -l 库名不带 lib 前缀。

【讨论】:

  • 当您的平台上存在具有相同功能的标准标志时,使用非标准标志不是一个好主意。
【解决方案3】:

在日食中

properties->c/c++Build->setting->GCC C++ linker->libraries in top part add "pthread"

【讨论】:

  • 同样的技巧适用于 code::project (我认为其他 IDE 也是如此)
  • 我知道这可能有点晚了,但是。如果您在属性中找不到 C/C++ Build 设置(我找不到,可能是由于安装或错误),那么使用 CMakeLists.txt 文件有一个直接的较低级别的解决方法。您需要在 add_executable 命令之前插入SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")。这将指示链接器执行相同的操作(请参阅 CMAKE_EXE_LINKER_FLAGSSET 文档以获取更多帮助)。
【解决方案4】:

从 Linux 终端运行,对我有用的是使用以下命令进行编译(假设我要编译的 c 文件名为 test.c):

gcc -o test test.c -pthread

希望它对某人有所帮助!

【讨论】:

  • 在 Ubuntu 16.x 中确认
【解决方案5】:

如果你使用的是cmake,你可以使用:

add_compile_options(-pthread)

或者

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

【讨论】:

    【解决方案6】:

    我相信在CMake 中添加pthread 的正确方法是如下

    find_package (Threads REQUIRED)
    
    target_link_libraries(helloworld
        ${CMAKE_THREAD_LIBS_INIT}
    )
    

    【讨论】:

    • 这具有相同的效果,但使用了 Threads::Threads 目标而不是 CMAKE_THREAD_LIBS_INIT 变量。 target_link_libraries(helloworld PUBLIC Threads::Threads)
    【解决方案7】:

    实际上,它给出了几个用于pthreads的编译命令示例,代码如下表所示,如果您继续阅读以下教程:

    https://computing.llnl.gov/tutorials/pthreads/#Compiling

    【讨论】:

      【解决方案8】:

      这样编译:gcc demo.c -o demo -pthread

      【讨论】:

        【解决方案9】:

        在 Visual Studio 2019 中,在以下项目的属性页中指定 -pthread

        链接器 -> 命令行 -> 附加选项

        在文本框中输入-pthread

        【讨论】:

        • 当我这样做时,我收到错误“pthread:没有这样的文件或目录”
        【解决方案10】:

        您需要在 gcc 中使用选项 -lpthread

        【讨论】:

        • 错误信息! -lpthread 不是“选项”,它指定了一个库。
        • 命令行选项(与命令行参数相反)
        【解决方案11】:

        您只需要在 proprieties=>C/C++ build=>GCC C++ Linker=>Libraries=> 顶部“Libraries(-l)”中添加“pthread”。 就是这样

        【讨论】:

          【解决方案12】:

          由于没有一个答案完全满足我的需求(使用 MSVS 代码),因此我也在这里添加了我使用此 IDE 和 CMAKE 构建工具的经验。

          第 1 步:确保在您的 .cpp(或 .hpp,如果需要)中包含:

          #include <functional>
          

          步骤 2 对于 MSVSCode IDE 用户: 将此行添加到您的 c_cpp_properties.json 文件中:

          "compilerArgs": ["-pthread"],
          

          第 2 步对于 CMAKE 构建工具用户: 将此行添加到您的 CMakeLists.txt

          set(CMAKE_CXX_FLAGS "-pthread")
          

          注意:添加标志 -lpthread(而不是 -pthread)会导致链接失败。

          【讨论】:

            【解决方案13】:

            检查手册页,你会得到。

            使用 -pthread 编译和链接。

            SYNOPSIS
                   #include <pthread.h>
            
                   int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                                      void *(*start_routine) (void *), void *arg);
            
            
                   Compile and link with -pthread.
                   ....
            

            【讨论】:

              【解决方案14】:

              在 Anjuta 中,转至 Build 菜单,然后选择 Configure Project。 在配置选项框中,添加:

              LDFLAGS='-lpthread'
              

              希望它也能帮助别人......

              【讨论】:

                【解决方案15】:

                有时,如果您使用多个库,请检查库依赖关系。 (例如 -lpthread -lSDL... ... -lSDL -lpthread)

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2011-08-16
                  • 1970-01-01
                  • 2013-06-20
                  • 1970-01-01
                  • 2016-03-12
                  相关资源
                  最近更新 更多