13

GCC 4.7.2 でいくつかの新しい C++11 機能を試しているところですが、実行しようとすると seg fault が発生します。

$ ./a.out
Message from main.
terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1
Aborted (core dumped)

c++0xに関して、GCCの「ベータ」機能でコンパイルしました:

g++ -std=c++11 c11.cpp

コード:

#include <future>
#include <iostream>

void called_from_async() {
  std::cout << "Async call" << std::endl;
}

int main() {
  //called_from_async launched in a separate thread if possible
  std::future<void> result( std::async(called_from_async));

  std::cout << "Message from main." << std::endl;

  //ensure that called_from_async is launched synchronously 
  //if it wasn't already launched
  result.get();

  return 0;
}
4

1 に答える 1

23

これは、POSIX スレッド ライブラリとのリンクを忘れたためだと思います。フラグに-pthreador-lpthreadを追加するだけで、問題は解決するはずです。g++

詳細に興味がある場合、これは、 pthreadこれらの機能を使用した場合にのみ、C++11 ランタイムがランタイムからシンボルを解決するために発生します。したがって、リンクを忘れると、ランタイムはこれらのシンボルを解決できず、環境をスレッドをサポートしていないかのように扱い、例外をスローします (これをキャッチせず、アプリケーションを中止します)。

于 2012-10-18T20:09:37.503 に答える