web-dev-qa-db-fra.com

Comment compiler C ++ 11 avec Clang 3.2 sur OSX Lion?

J'essaie de compiler le programme C++ suivant qui repose sur le C++ 11 <thread> entête. J'essaie de faire cela sur Osx Lion.

#include <iostream>
#include <thread>
#include <vector>

void hello()
{
    std::cout << "Hello from thread " << std::this_thread::get_id() << std::endl;
}

int main()
{
    std::vector<std::thread> threads;

    for(int i = 0; i < 5; i++)
    {
        threads.Push_back(std::thread(hello));
    }

    for(auto& thread: threads)
    {
        thread.join();
    }

    return 0;
}

Le programme ci-dessus compile parfaitement avec G ++ 4.7 que j'ai installé à l'aide du gestionnaire de packages homebrew. Toutefois, lorsque vous essayez de compiler le programme ci-dessus avec Clang 3.2 (également installé à l'aide du gestionnaire de packages Homebrew), je reçois le message d'erreur suivant:

Zameers-MacBook-Air:tmp zmanji$ clang++ -v -std=c++11 test.cpp 
clang version 3.2 (tags/RELEASE_32/final)
Target: x86_64-Apple-darwin11.3.0
Thread model: posix
 "/usr/local/Cellar/llvm/3.2/bin/clang" -cc1 -triple x86_64-Apple-macosx10.7.0 -emit-obj -mrelax-all -disable-free -main-file-name test.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 128.2 -v -resource-dir /usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2 -fmodule-cache-path /var/folders/qf/j_7_sw0n093gn1y0mtsyztxh0000gn/T/clang-module-cache -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Users/zmanji/tmp -ferror-limit 19 -fmessage-length 101 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.7.0 -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/qf/j_7_sw0n093gn1y0mtsyztxh0000gn/T/test-Zkjucl.o -x c++ test.cpp
clang -cc1 version 3.2 based upon LLVM 3.2svn default target x86_64-Apple-darwin11.3.0
ignoring nonexistent directory "/usr/include/c++/4.2.1/i686-Apple-darwin10/x86_64"
ignoring nonexistent directory "/usr/include/c++/4.0.0"
ignoring nonexistent directory "/usr/include/c++/4.0.0/i686-Apple-darwin8/"
ignoring nonexistent directory "/usr/include/c++/4.0.0/backward"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.2.1
 /usr/include/c++/4.2.1/backward
 /usr/local/include
 /usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
test.cpp:2:10: fatal error: 'thread' file not found
#include <thread>
         ^
1 error generated.

Il semble que Clang ne puisse pas trouver le <thread> en-tête mais je ne suis pas sûr de savoir pourquoi.

éditer : J'ai essayé la réponse de la Shar ci ci-dessous et je reçois maintenant l'erreur que le <iostream> L'en-tête n'existe pas.

Zameers-MacBook-Air:tmp zmanji$ clang++ -v -std=c++11 -stdlib=libc++ test.cpp 
clang version 3.2 (tags/RELEASE_32/final)
Target: x86_64-Apple-darwin11.3.0
Thread model: posix
 "/usr/local/Cellar/llvm/3.2/bin/clang" -cc1 -triple x86_64-Apple-macosx10.7.0 -emit-obj -mrelax-all -disable-free -main-file-name test.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 128.2 -v -resource-dir /usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2 -fmodule-cache-path /var/folders/qf/j_7_sw0n093gn1y0mtsyztxh0000gn/T/clang-module-cache -stdlib=libc++ -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Users/zmanji/tmp -ferror-limit 19 -fmessage-length 203 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.7.0 -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/qf/j_7_sw0n093gn1y0mtsyztxh0000gn/T/test-k2Alf4.o -x c++ test.cpp
clang -cc1 version 3.2 based upon LLVM 3.2svn default target x86_64-Apple-darwin11.3.0
ignoring nonexistent directory "/usr/local/Cellar/llvm/3.2/bin/../lib/c++/v1"
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/local/Cellar/llvm/3.2/bin/../lib/clang/3.2/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
         ^
1 error generated.
22
Zameer Manji

Vous devez utiliser libc ++ au lieu de libstdc ++ .

clang++ -std=c++11 -stdlib=libc++ foo.cc
44
Bill Lynch