mac下c++多线程编程提示No matching constructor for initialization of ‘std::thread‘

        程序中想创建个可传参的子线程:

void func(int a)
{
    do sth...
}

int main()
{
    int a = 5;
    std::thread thread(func, a);
    thread.detach();
}

        但是编译时 xcode 提示:

No matching constructor for initialization of 'std::thread'

        具体来看是 thread 构造函数只支持一个参数,而程序中传了两个:

Candidate constructor not viable: requires 1 argument, but 2 were provided

        这是因为 mac 下的 g++ 使用的是 clang,默认不支持 c++11多线程。

lucas@lucasdeMacBook-Pro premiereproplugin % g++ -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.5 (clang-1205.0.22.11)
Target: x86_64-apple-darwin20.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

        可在 xcode 中做如下修改,即添加编译指令:-std=c++11

        如果是 vscode 呢?可在配置文件 tasks.jsonargs 下同样加上:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ 生成活动文件",
            "command": "/usr/bin/clang++",
            "args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-std=c++11",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            ...
        }
    ],
    "version": "2.0.0"
}

       

        以上,欢迎交流~ 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值