CMAKE, Clang and C++v11 on OS X 10.8

16,443

You need to provide the -std=c++11 and -stdlib=libc++ flags to the compiler in order to fully activate its C++11 support. This can be done through ccmake (turn on advanced mode (with t), and set CMAKE_CXX_FLAGS to -std=c++11 -stdlib=libc++), or through an equivalent directive in your CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.9)
set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++")
project(Test)
add_executable(Test main.cpp)
Share:
16,443
Jasmine
Author by

Jasmine

Updated on June 07, 2022

Comments

  • Jasmine
    Jasmine almost 2 years

    OS X 1.8

    CMAKE 2.8.9

    Clang $ clang -v Apple clang version 4.0 (tags/Apple/clang-421.10.60) (based on LLVM 3.1svn) Target: x86_64-apple-darwin12.0.0 Thread model: posix

    CMAKELists.txt:

    cmake_minimum_required (VERSION 2.8.9)
    project (Test)
    add_executable(Test main.cpp)
    

    main.cpp

    //Create a C++11 thread from the main program
    #include <iostream>
    #include <thread>
    
    //This function will be called from a thread
    void call_from_thread() {
        std::cout << "Hello, World!" << std::endl;
    }
    
    int main() {
        //Launch a thread
        std::thread t1(call_from_thread);
    
        //Join the thread with the main thread
        t1.join();
    
        return 0;
    }
    

    My error:

    $ make
    Scanning dependencies of target Test
    [100%] Building CXX object CMakeFiles/Test.dir/main.cpp.o
    test/main.cpp:4:10: fatal error: 'thread' file not found
    #include <thread>
         ^
    1 error generated.
    make[2]: *** [CMakeFiles/Test.dir/main.cpp.o] Error 1
    make[1]: *** [CMakeFiles/Test.dir/all] Error 2
    make: *** [all] Error 2
    

    So does the version of Clang not support C++v11 features? This same program does compile under gcc-4.7.1 on OSX 10.8

    This reference says it should work http://www.cpprocks.com/a-comparison-of-c11-language-support-in-vs2012-g-4-7-and-clang-3-1/

    What am I doing wrong?

  • Jasmine
    Jasmine over 11 years
    Thanks: set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++")
  • Michael
    Michael about 10 years
    That still doesn't work. The options are passed to the compiler, "make VERBOSE=1" verified that, but C++11 classes such as std::thread or std::function are undefined, despite <thread> and <functional> being included.
  • aledalgrande
    aledalgrande almost 10 years
    @Michael did you find a solution?
  • Mankarse
    Mankarse almost 10 years
    @aledalgrande: Perhaps you also need set(CMAKE_EXE_LINKER_FLAGS "-stdlib=libc++")?
  • aledalgrande
    aledalgrande almost 10 years
    set(CMAKE_CXX_FLAGS "-stdlib=libc++") was what worked for me
  • redcurry
    redcurry over 9 years
    set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++") and set(CMAKE_EXE_LINKER_FLAGS "-stdlib=libc++") together worked for me. I was running OS X 10.7.5 and LLVM version 4.2 (clang-425.0.28).