使用g++编译多线程代码(-Wl,--no根据需要不工作)

Compiling multithread code with g++ (-Wl,--no-as-needed NOT working)

本文关键字:工作 --no 编译 g++ 多线程 代码 -Wl 使用      更新时间:2023-10-16

我的问题实际上在这里描述:用g++编译多线程代码。但是,关于使用"-Wl,--no as needed"解决问题的答案对我不起作用

我也在不同的订单中添加了-Wl,--no-as-needed -pthread -std=c++0x,但我仍然得到:

terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted"

该怎么办?

Info: 
Ubuntu 12.04LTS
Running Eclipse CDT
g++ v4.8.1 

编辑:我尝试用-Wl,--no-as-needed -lpthread -std=c++0x进行构建,但没有成功。代码:

#include <iostream>
#include <chrono>
#include <thread>
void foo()
{
    std::cout << "Thread 1 created.." << std::endl;
}
int main()
{
    std::thread t1(foo);
    t1.join();
    return 0;
}

编辑:不幸的是,你的建议都不起作用。我决定改用Boost

  1. 它是-Wl,--no-as-needed而不是-Wl,--no_as_needed,您使用连字符
  2. -pthread是编译器的标志,而不是链接器的标志,链接器的正确标志是-lpthread
  3. Mingw并不总是带有相同的线程库,Mingw有不止一个多线程选项,你应该根据你的Mingw构建来记录自己
g++  filename.c -std=c++11 -lpthread

我正在用上面的命令编译你的代码,它的工作非常完美。