2

我正在阅读有关 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::operator<<(long)' thread_test.cpp:(.text+0x33 ): 未定义对std::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::operator<<(std::ostream& (*)(std::ostream&))' /tmp/ccmpQLyp.o: 在函数main': 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::operator<<(int)' thread_test.cpp:(.text+0x7a): 未定义引用std::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::operator<<(std::ostream& ( *)(std::ostream&))' thread_test.cpp:(.text+0xcc): 未定义引用std::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()' 的未定义引用

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

4

3 回答 3

4

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

于 2013-04-23T11:30:13.753 回答
3

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

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

于 2020-01-04T18:19:45.033 回答
0

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

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

像:g++ thread.cc -lpthread -o thread

只需在库名称前加上-l

于 2022-02-16T16:03:52.093 回答