Note that there are some explanatory texts on larger screens.

plurals
  1. POCMake not working with C++11 on OSX
    text
    copied!<p>I've just upgraded to Mountain Lion so I can use some of the C++11 features on the new version of Clang that comes with xcode. I'm using cmake 2.8.9 from Homebrew.</p> <p>I've made a very simple CMake project which adds the compiler flags for C++11:</p> <pre><code># CMakeLists.txt cmake_minimum_required(VERSION 2.8) add_executable(test test.cxx) add_definitions(-std=c++0x -stdlib=libc++) </code></pre> <p>where the C++ code in test.cxx is the following:</p> <pre><code>#include &lt;iostream&gt; int main() { std::cout &lt;&lt; "Howdy" &lt;&lt; std::endl; return 0; } </code></pre> <p>When running cmake and make, the file compiles just fine but then the linker outputs the following errors:</p> <pre><code>Linking CXX executable test Undefined symbols for architecture x86_64: "std::__1::locale::use_facet(std::__1::locale::id&amp;) const", referenced from: std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp; std::__1::endl&lt;char, std::__1::char_traits&lt;char&gt; &gt;(std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp;) in test.cxx.o "std::__1::ios_base::getloc() const", referenced from: std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp; std::__1::endl&lt;char, std::__1::char_traits&lt;char&gt; &gt;(std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp;) in test.cxx.o "std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;::put(char)", referenced from: std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp; std::__1::endl&lt;char, std::__1::char_traits&lt;char&gt; &gt;(std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp;) in test.cxx.o "std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;::flush()", referenced from: std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp; std::__1::endl&lt;char, std::__1::char_traits&lt;char&gt; &gt;(std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp;) in test.cxx.o "std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;::sentry::sentry(std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp;)", referenced from: std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp; std::__1::operator&lt;&lt;&lt;std::__1::char_traits&lt;char&gt; &gt;(std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp;, char const*) in test.cxx.o "std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;::sentry::~sentry()", referenced from: std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp; std::__1::operator&lt;&lt;&lt;std::__1::char_traits&lt;char&gt; &gt;(std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp;, char const*) in test.cxx.o "std::__1::cout", referenced from: _main in test.cxx.o "std::__1::ctype&lt;char&gt;::id", referenced from: std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp; std::__1::endl&lt;char, std::__1::char_traits&lt;char&gt; &gt;(std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp;) in test.cxx.o "std::__1::locale::~locale()", referenced from: std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp; std::__1::endl&lt;char, std::__1::char_traits&lt;char&gt; &gt;(std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp;) in test.cxx.o "std::__1::ios_base::__set_badbit_and_consider_rethrow()", referenced from: std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp; std::__1::operator&lt;&lt;&lt;std::__1::char_traits&lt;char&gt; &gt;(std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp;, char const*) in test.cxx.o "std::__1::ios_base::clear(unsigned int)", referenced from: std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp; std::__1::operator&lt;&lt;&lt;std::__1::char_traits&lt;char&gt; &gt;(std::__1::basic_ostream&lt;char, std::__1::char_traits&lt;char&gt; &gt;&amp;, char const*) in test.cxx.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make[2]: *** [test] Error 1 make[1]: *** [CMakeFiles/test.dir/all] Error 2 make: *** [all] Error 2 </code></pre> <p>I get no errors if I comment out the add_definitions line in the CMakeLists.txt file, and I can also avoid errors if I remove the std::cout line in test.cxx. Perhaps the weirdest part of all is that if I simply run</p> <pre><code>clang++ -std=c++0x -stdlib=libc++ test.cxx </code></pre> <p>it compiles just fine! So, following a commentor's advice, I checked the actual commands cmake is running for compiling and linking.</p> <p>Compile:</p> <pre><code>/usr/bin/c++ -std=c++0x -stdlib=libc++ -o CMakeFiles/test.dir/test.cxx.o -c /Users/luis/test.cxx </code></pre> <p>Link:</p> <pre><code>/usr/bin/c++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/test.dir/test.cxx.o -o test </code></pre> <p>The main issue now seems to be that the linker is not supplying the proper C++11 flags. Is there a better way to supply these flags so that both the compiler and the linker will use them?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload