32

Macportsを使用してgcc4.4をコンパイルしてインストールしました。

-> g++ -g -Wall -ansi -pthread -std=c++0x main.cpp... を使用してコンパイルしようとすると:

 #include <thread>
 ...
  std::thread t(handle);
  t.join();
 ....

コンパイラは次を返します。

 cserver.cpp: In member function 'int CServer::run()':
 cserver.cpp:48: error: 'thread' is not a member of 'std'
 cserver.cpp:48: error: expected ';' before 't'
 cserver.cpp:49: error: 't' was not declared in this scope

しかし、std::cout <<...うまくコンパイルされます..

誰でも私を助けることができますか?

4

3 に答える 3

15

gcc はまだ std::thread を完全にはサポートしていません:

http://gcc.gnu.org/projects/cxx0x.html

http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html

当面はboost::threadを使用してください。

編集

以下はgcc 4.4.3でコンパイルして正常に実行されましたが:

#include <thread>
#include <iostream>

struct F
{
  void operator() () const
  {
    std::cout<<"Printing from another thread"<<std::endl;
  }
};

int main()
{
  F f;
  std::thread t(f);
  t.join();

  return 0;
}

でコンパイル

g++ -Wall -g -std=c++0x -pthread main.cpp

の出力a.out:

別のスレッドからの印刷

完全なコードを提供できますか? たぶん、それらの中に潜んでいるあいまいな問題があり...ますか?

于 2010-03-26T03:24:12.467 に答える
6

-ansiを削除します。これは、-std = c ++ 98を意味しますが、これは明らかに望ましくありません。また、マクロ__STRICT_ANSI__が定義されるため、C ++ 0xサポートを無効にするなどして、ヘッダーの動作が変わる可能性があります。

于 2010-03-25T21:50:46.087 に答える
6

MinGWを使用しているWindowsでも同じ問題がありました。github mingw-std-threadsで、mingw.mutex.h、mingw.thread.h ファイルをグローバル MinGW ディレクトリにインクルードするためのラッパー クラスが見つかりました。この問題は修正されました。私がしなければならなかったのは、ヘッダーファイルをインクルードすることだけで、私のコードは同じままでした

#include "mingw.thread.h"

...
std::thread t(handle);
...
于 2016-11-30T19:49:06.973 に答える