C++ CodeLite 启用多线程?

C++ CodeLite Enable Multithreading?

当我尝试编译任何在 CodeLite 中使用多线程的程序时,我遇到了一些奇怪的行为。

我收到错误:

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

经过一些快速谷歌搜索后,我发现我必须将“-pthread”添加到编译器选项。

注意:CodeLite 将 -l 放在库的前面,因此它确实使用了 -lpthread

在我清理并重建项目后,我仍然遇到错误。据我所知构建日志看起来不错?

真正令人沮丧的部分出现在我通过命令行手动编译它时,它工作得很好。

我搜索过,但 none 的解决方案似乎对我有用?也许我在某处错过了一步?

这是我的测试代码。 我还应该注意到我使用的是 Ubuntu14.04 和 CodeLite 9.1.0

#include <iostream>
#include <string>

#include <thread>

void test()
{
    std::cout << " Look it works! \n";
}

void int main(int argc, char** argv)
{
    std::thread thrd_1 = std::thread(test);
    thrd_1.join();

    return 0;
}

如有任何帮助,我们将不胜感激!

您在编译器选项中传递 -pthread。你需要通过它 在链接器选项中,在您不需要的链接器选项中 将 pthread 指定为库。 -pthread 选项表示 尽一切可能链接此平台上的 posix 线程库

$ g++ -c -O0 -std=c++11 -o main.o main.cpp
$ g++ -o threadtest -pthread main.o
$ ./threadtest
 Look it works!