c++ - 来自 c++11 的 std::thread 问题

标签 c++ multithreading g++ std stdthread

我在尝试使用标准模板库编译多线程程序时遇到了一些麻烦。 当我尝试编译以下程序时,它返回一个模糊的错误:

#include <iostream>
#include <thread>

void foo()
{
    std::cout << "Thread 1\n";
}

int main(int argc, char** argv)
{
    std::thread tr(foo);
    std::cout << "Main thread\n";
    tr.join();

    return 0;
}

我不明白错误:

/tmp/ccE8EtL1.o : In the function « std::thread::thread<void (&)()>(void (&)()) » :
 file.cpp:(.text._ZNSt6threadC2IRFvvEJEEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEEEOT_DpOT0_]+0x21) : undefined reference to « pthread_create »
  collect2: error : ld has return 1 execution status code

我编译它:

g++ -std=c++14 file.cpp -o test -Wall

有人能帮帮我吗?

最佳答案

关于c++ - 来自 c++11 的 std::thread 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42046569/

相关文章:

linux - CMake - 如何避免链接器命令行中的 CMAKE_CXX_FLAGS

c++ - 在C++中将uint8_t数组转换为字符串

c++ - vector (反)序列化与 Boost.serialization 的向后兼容性

C++ 嵌套 Lambda

mysql - 在 MySQL 中处理大量数据 - 一次选择多少行?

php - 在 PHP 中同步和暂停线程

c++ - 当我请求一个数字但用户输入了一个非数字时,如何防止失控的输入循环?

.net - 与从非 GUI 线程显示 MessageBox 相关的问题

c++ - g++ 认为我没有传递引用

c++ - 如何在 Fedora 中编译简单的 allegro 文件?