linux - c++写线程程序时为什么要加-pthread编译选项

查看:82
本文介绍了linux - c++写线程程序时为什么要加-pthread编译选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

#include <iostream>
#include <thread>

using namespace std;

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

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

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

解决方案

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

pthreadlinux下的线程库,用了多线程就要链接这个库,这时候要在编译选项上增加-pthread或者-lpthread

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

这篇关于linux - c++写线程程序时为什么要加-pthread编译选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆