我是靠谱客的博主 飘逸鸡翅,最近开发中收集的这篇文章主要介绍c++ std:thread编译出错记录,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

https://coderwall.com/p/rzkw6q


Consider the following code, which uses the C++11 (used to be known as C++0x) thread library:

#include <iostream>
#include <thread>

using namespace std;

void hello_world()
{
  cout << "Hello from thread!n";
}

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

How can we get this to work? If we compile it with:

g++ bare.cpp -o bare

We'll get something like:

/usr/include/c++/4.5/bits/c++0x_warning.h:31:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.

... and maybe something about the thread type used in the code:

error: ‘thread’ was not declared in this scope

So we add the std=c++0x ...

g++ bare.cpp -std=c++0x -o bare

No errors during compilation, but when we try to execute bare...

$ ./bare
terminate called after throwing an instance of 'std::system_error'
  what():  
Aborted

... oops. We have to link this against the operating system's implementation of threads. In linux, this is the libpthread library. The winning command is:

g++ bare.cpp -std=c++0x -lpthread -o bare

and then:

$ ./bare
Hello from thread!

最后

以上就是飘逸鸡翅为你收集整理的c++ std:thread编译出错记录的全部内容,希望文章能够帮你解决c++ std:thread编译出错记录所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部