【问题标题】:Error trying to use pthread on Ubuntu尝试在 Ubuntu 上使用 pthread 时出错
【发布时间】:2013-04-16 14:26:36
【问题描述】:

我正在阅读有关 C++ 线程的教程并测试了以下代码:

#include <iostream>
#include <pthread.h>
#include <cstdlib>

using namespace std;

#define NUM_THREADS     5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   cout << "Hello World! Thread ID, " << tid << endl;
   pthread_exit(NULL);
}

int main ()
{
   pthread_t threads[NUM_THREADS];
   int rc;
   int i;
   for( i=0; i < NUM_THREADS; i++ ){
      cout << "main() : creating thread, " << i << endl;
      rc = pthread_create(&threads[i], NULL, 
                      PrintHello, &threads[i]);
      if (rc){
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

我尝试使用 gcc 和 g++ 编译这段代码,但我总是遇到编译错误。

使用 gcc -pthread thread_test.c:

/tmp/ccmpQLyp.o:在函数PrintHello(void*)': thread_test.cpp:(.text+0x1a): undefined reference tostd::cout' thread_test.cpp:(.text+0x1f): 未定义引用std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' thread_test.cpp:(.text+0x2e): undefined reference tostd::ostream::operatorstd::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' thread_test.cpp:(.text+0x3b): undefined reference tostd::ostream::operatormain': thread_test.cpp:(.text+0x63): undefined reference tostd::cout' thread_test.cpp:(.text+0x68): 未定义引用std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' thread_test.cpp:(.text+0x75): undefined reference tostd::ostream::operatorstd::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' thread_test.cpp:(.text+0x82): undefined reference tostd::ostream::operatorstd::cout' thread_test.cpp:(.text+0xd1): undefined reference tostd::basic_ostream >& std::operator(std::basic_ostream >&, char const*)' thread_test.cpp:(.text+0xde): 未定义引用std::ostream::operator<<(int)' thread_test.cpp:(.text+0xe3): undefined reference tostd::basic_ostream >& std::endl >(std::basic_ostream >&)' thread_test.cpp:(.text+0xeb): 对std::ostream::operator<<(std::ostream& (*)(std::ostream&))' /tmp/ccmpQLyp.o: In function__static_initialization_and_destruction_0(int, int)' 的未定义引用: thread_test.cpp:(.text+0x141): 未定义引用std::ios_base::Init::Init()' thread_test.cpp:(.text+0x150): undefined reference tostd::ios_base::Init::~Init()' /tmp/ccmpQLyp.o:(.eh_frame+0x47): 未定义对 `__gxx_personality_v0' 的引用 collect2:错误:ld 返回 1 个退出状态

你能帮忙吗?我必须做些什么才能让这段代码在 Linux 和 Windows 上运行吗?

【问题讨论】:

  • 改用-lpthread
  • 使用 gcc 和 -lpthread 我仍然得到错误:/tmp/ccq3Kk7G.o: In function main': thread_test.cpp:(.text+0xb9): undefined reference to pthread_create' collect2: error: ld returned 1 exit status juliano@juliano-linux:~/Documents /cpp$ gcc -lpthread thread_test.cpp /tmp/ccVu4YcA.o: 在函数PrintHello(void*)': thread_test.cpp:(.text+0x1a): undefined reference to std::cout' thread_test.cpp:(.text+0x1f): 未定义引用`std::basic_ostream >& std::operator >(std::basic_ostream >&, char const*)'
  • gcc -o thread_test.c -Wall -Werror -lpthread
  • @SChepurin:/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:在函数_start': (.text+0x20): undefined reference to main' collect2: error: ld 返回 1 个退出状态

标签: c++ gcc g++ pthreads


【解决方案1】:

使用g++ 代替gcc,或手动链接-lstdc++

【讨论】:

  • 使用 g++ 我收到以下错误:/tmp/ccq3Kk7G.o: In function main': thread_test.cpp:(.text+0xb9): undefined reference to pthread_create' collect2: error: ld returned 1 exit status
【解决方案2】:

对于 2020 年阅读本文的任何人:

在 GCC 中,当链接到 pthread 时,不要使用“-lpthread”,而只使用“-pthread” CLI 选项。

【讨论】:

  • 谢谢。让我在 Ubuntu 20.04、g++ 8 上解决了这个错误
【解决方案3】:

了解 C++ 编译器如何链接库文件很重要。

如果我们不明确告诉编译器在哪里寻找库头文件,那么它会自动在/usr/local/include 中查找。但是,如果编译器正在寻找的头文件不存在,那么它将产生一个错误,就像你所面临的那样(即 pthread 库存在于/usr/local/lib 而不是/usr/local/include)。所以,你需要告诉编译器它需要查看/usr/local/lib内部:

点赞:g++ thread.cc -lpthread -o thread

只需在库名称前加上-l

【讨论】:

    猜你喜欢
    • 2020-08-23
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多