简单线程上的C ++ std :: thread编译错误

问题描述

这是我的第一个问题,我希望张贴正确。 我正在尝试编译这个我第一次使用std :: thread

的简单程序
#include <thread>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>


bool isOdd (int i) { return ((i%2)==1); }

void countOdd(std::vector<int>::iterator first,std::vector<int>::iterator last,int& count)
{
    count = count_if(first,last,isOdd);
}


int main()
{
    std::vector<int> myvector;
    for (int i=0; i<1000000; i++) myvector.push_back(i);
    int count = 0;
    std::thread(countOdd,myvector.begin(),myvector.end(),std::ref(count));
    std::cout << count << std::endl;
    return 0;
}

但是我得到了错误

 g++ multithreadExample.cpp -o multithreadExample
/tmp/ccaJls0z.o: nella funzione "std::thread::thread<void (&)(__gnu_cxx::__normal_iterator<int*,std::vector<int,std::allocator<int> > >,__gnu_cxx::__normal_iterator<int*,int&),std::reference_wrapper<int> >(void (&)(__gnu_cxx::__normal_iterator<int*,std::allocator<int> > >&&,std::reference_wrapper<int>&&)":
multithreadExample.cpp:(.text._ZNSt6threadC2IRFvN9__gnu_cxx17__normal_iteratorIPiSt6vectorIiSaIiEEEES7_RiEJS7_S7_St17reference_wrapperIiEEEEOT_DpOT0_[_ZNSt6threadC5IRFvN9__gnu_cxx17__normal_iteratorIPiSt6vectorIiSaIiEEEES7_RiEJS7_S7_St17reference_wrapperIiEEEEOT_DpOT0_]+0x42): riferimento non definito a "pthread_create"
collect2: error: ld returned 1 exit status

我正在使用Ubuntu 18.04

g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

有人猜吗? 预先非常感谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)