简体   繁体   中英

C++ CodeLite Enable Multithreading?

I'm experiencing a bit of strange behaviour when I try to compile any program that uses multithreading in CodeLite.

I get the error:

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

after some quick googling I found out I had to add "-pthread" to the compiler options.

Codelite 选项的图像。 链接器选项

Note: that CodeLite puts -l in front of libraries, so it does use -lpthread

After I clean, and rebuild the project, I still get the error though. As far as I can tell the build log looks fine?

CodeLite 构建日志的图像。

The truly frustrating part comes about when I compile it manually via the command line, It works just fine.

手动编译工作

I've searched, but none of the solutions seem to work for me? perhaps I've missed a step somewhere?

here's my test code. I should also note I'm using Ubuntu14.04, and 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;
}

Any help would be greatly appreciated!

You are passing -pthread in the compiler options. You need to pass it in the linker options, and in the linker options you do not need to specify pthread as a library. The -pthread option means do whatever it is that links the posix threads library on this platform .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM