用g ++编译multithreading代码(-Wl, – 不需要不工作)

我的问题实际上是在这里描述: 用g ++编译multithreading代码 。 但是关于使用“-Wl, – 不需要”的解决方法的答案不适用于我。

我已经添加了-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构build-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; } 

编辑:所以不幸的是你的build议没有工作。 我决定使用Boost来代替。

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

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