linux - c++写线程程序时为什么要加-pthread编译选项
PHP中文网
PHP中文网 2017-04-17 16:08:40
0
2
493
#include <iostream>
#include <thread>

using namespace std;

void func()
{
    cout << "hello world";
}

int main()
{
    thread t(func);
    t.join();
    
    return 0;
}

为什么要加上-pthread编译选项才能链接成功呢?

PHP中文网
PHP中文网

认证0级讲师

reply all(2)
洪涛

-pthread Adds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker.

pthread is a thread library under linux. If you use multi-threading, you need to link to this library. At this time, you need to add -pthread to the compilation options. Or -lpthreadpthreadlinux下的线程库,用了多线程就要链接这个库,这时候要在编译选项上增加-pthread或者-lpthread

-pthread选项对 预处理器和链接器起作用
而老式的-lpthread只对链接器起作用
推荐使用-pthread

-pthread option works on preprocessor and linker
while the old -lpthread only works on linker
It is recommended to use - pthread🎜
伊谢尔伦

POSIX threads, or Pthreads for short, is the POSIX standard for threads. This standard defines a complete set of APIs for creating and manipulating threads. In Unix-like operating systems (Unix, Linux, Mac OS X, etc.), Pthreads are used as the threads of the operating system.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!