Xcode 4.3 and C++11 include paths

28,495

Solution 1

You need:

-std=c++0x

to select C++11. And you need:

-stdlib=libc++

to select libc++. By default, the std::lib that shipped with gcc 4.2 is used, which is pre-C++11.

Solution 2

Howard Hinnant's answer (with corrections) is the correct answer for the command line.

To use the new C++11 standard library inside of Xcode:

  • In the Build Settings tab for your project, scroll down to "Apple LLVM Compiler 4.1 - Language"
  • Set the setting "C++ Language Dialect" to "C++11 [-std=c++11]"
  • Set the setting "C++ Standard Library" to "libc++ (LLVM standard C++ library with C++11 support)"
Share:
28,495

Related videos on Youtube

StackedCrooked
Author by

StackedCrooked

Software developer specializing in C++ cross-platform development. Hobby projects: Coliru: online C++ compiler Anime Ratings: Google Chrome extension for finding good anime tetris-challenge: experimentation with Tetris AI algorithms Clojure notes: just my notes :)

Updated on January 13, 2020

Comments

  • StackedCrooked
    StackedCrooked over 4 years

    I installed Xcode 4.3 and want to test this C++11 program:

    #include <type_traits>
    
    int main()
    {
    }
    

    However, it doesn't find the type_traits header:

    ~ $ c++ -o test main.cpp
    main.cpp:1:10: fatal error: 'type_traits' file not found
    #include <type_traits>
             ^
    1 error generated.
    

    It seems that I am using the correct compiler:

    ~ $ c++ -v
    Apple clang version 3.1 (tags/Apple/clang-318.0.45) (based on LLVM 3.1svn)
    Target: x86_64-apple-darwin11.3.0
    Thread model: posix
    

    I checked the default include paths:

    ~ $ `c++ --print-prog-name=cc1plus` -v
    ignoring nonexistent directory "/usr/include/c++/4.2.1/i686-apple-darwin11"
    ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/../../../../i686-apple-darwin11/include"
    #include "..." search starts here:
    #include <...> search starts here:
     /usr/include/c++/4.2.1
     /usr/include/c++/4.2.1/backward
     /usr/local/include
     /Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/include
     /usr/include
     /System/Library/Frameworks (framework directory)
     /Library/Frameworks (framework directory)
    End of search list.
    

    Above paths do indeed not contain the type_traits headers. A search command reveals that can be found in two locations:

    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/usr/include/c++/v1/type_traits
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1/type_traits
    

    Apparently something is wrong with my compiler defaults. How can I configure my compiler so that it finds the type_traits header in the right location?

    Update

    Following @sehe's suggestion:

    ~ $ clang++ -v -fshow-source-location -std=c++0x main.cpp
    Apple clang version 3.1 (tags/Apple/clang-318.0.45) (based on LLVM 3.1svn)
    Target: x86_64-apple-darwin11.3.0
    Thread model: posix
     "/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.7.3 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name main.cpp -pic-level 1 -mdisable-fp-elim -relaxed-aliasing -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 128.2 -v -resource-dir /usr/bin/../lib/clang/3.1 -fmodule-cache-path /var/folders/d6/sf96r2ps457230x3v8yj52s40000gp/T/clang-module-cache -std=c++0x -fdeprecated-macro -fdebug-compilation-dir /Users/francis -ferror-limit 19 -fmessage-length 174 -stack-protector 1 -fblocks -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-dispatch-method=mixed -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/d6/sf96r2ps457230x3v8yj52s40000gp/T/main-sUcT7k.o -x c++ main.cpp
    clang -cc1 version 3.1 based upon llvm 3.1svn 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/bin/../lib/clang/3.1/include
     /usr/include
     /System/Library/Frameworks (framework directory)
     /Library/Frameworks (framework directory)
    End of search list.
    main.cpp:1:10: fatal error: 'type_traits' file not found
    #include <type_traits>
             ^
    1 error generated.
    

    It doesn't seem to look in the Xcode.app bundle at all.

    One possible reason is that I installed both Xcode and the "Command line tools for Xcode". The latter installed binaries in the /usr folder.

    I just found that the type_traits header can also be found in /usr/include :

    ~ $ find /usr/include -type f -name type_traits
    /usr/include/c++/4.2.1/tr1/type_traits
    /usr/include/c++/v1/type_traits
    
  • rob mayoff
    rob mayoff about 12 years
    The test program in the question compiles with just the -stdlib=libc++ flag. The -std=c++0x flag isn't required to make it compile (though it's appropriate). Also, Xcode 4.3's clang allows the flag -std=c++11.
  • Howard Hinnant
    Howard Hinnant about 12 years
    @robmayoff: Right. libc++ will work (not quite as well) in C++03 mode. I hadn't noticed -std=c++11, thanks! I'll start using and recommending that.
  • RichardLiu
    RichardLiu about 12 years
    Confirmed with "c++11" option; tested with "clang++ -std=c++11 -stdlib=libc++ -o test main.cpp" command and compiled without problem.
  • Ionoclast Brigham
    Ionoclast Brigham almost 11 years
    Was getting link errors until I set these specific settings for my project.