简体   繁体   English

CMake无法在OSX上使用C ++ 11

[英]CMake not working with C++11 on OSX

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. 我刚刚升级到Mountain Lion,所以我可以在xcode附带的新版Clang上使用一些C ++ 11特性。 I'm using cmake 2.8.9 from Homebrew. 我正在使用Homebrew的cmake 2.8.9。

I've made a very simple CMake project which adds the compiler flags for C++11: 我做了一个非常简单的CMake项目,它为C ++ 11添加了编译器标志:

# CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
add_executable(test test.cxx)
add_definitions(-std=c++0x -stdlib=libc++)

where the C++ code in test.cxx is the following: test.cxx中的C ++代码如下:

#include <iostream>

int main()
{
  std::cout << "Howdy" << std::endl;
  return 0;
}

When running cmake and make, the file compiles just fine but then the linker outputs the following errors: 运行cmake和make时,文件编译得很好,但链接器输出以下错误:

Linking CXX executable test
Undefined symbols for architecture x86_64:
  "std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test.cxx.o
  "std::__1::ios_base::getloc() const", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test.cxx.o
  "std::__1::basic_ostream<char, std::__1::char_traits<char> >::put(char)", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test.cxx.o
  "std::__1::basic_ostream<char, std::__1::char_traits<char> >::flush()", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test.cxx.o
  "std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::sentry(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in test.cxx.o
  "std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::~sentry()", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in test.cxx.o
  "std::__1::cout", referenced from:
      _main in test.cxx.o
  "std::__1::ctype<char>::id", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test.cxx.o
  "std::__1::locale::~locale()", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in test.cxx.o
  "std::__1::ios_base::__set_badbit_and_consider_rethrow()", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in test.cxx.o
  "std::__1::ios_base::clear(unsigned int)", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, 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

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. 如果我在CMakeLists.txt文件中注释掉add_definitions行,我没有错误,如果我删除test.cxx中的std :: cout行,我也可以避免错误。 Perhaps the weirdest part of all is that if I simply run 也许最奇怪的部分是,如果我只是跑

clang++ -std=c++0x -stdlib=libc++ test.cxx

it compiles just fine! 它编译得很好! So, following a commentor's advice, I checked the actual commands cmake is running for compiling and linking. 因此,根据评论员的建议,我检查了cmake正在运行的实际命令,以进行编译和链接。

Compile: 编译:

/usr/bin/c++    -std=c++0x -stdlib=libc++ -o CMakeFiles/test.dir/test.cxx.o -c /Users/luis/test.cxx

Link: 链接:

/usr/bin/c++    -Wl,-search_paths_first -Wl,-headerpad_max_install_names   CMakeFiles/test.dir/test.cxx.o  -o test

The main issue now seems to be that the linker is not supplying the proper C++11 flags. 现在的主要问题似乎是链接器没有提供正确的C ++ 11标志。 Is there a better way to supply these flags so that both the compiler and the linker will use them? 是否有更好的方法来提供这些标志,以便编译器和链接器都使用它们?

I'm not sure that ADD_DEFINITIONS is the right tool for this particular job. 我不确定ADD_DEFINITIONS是否适合这项特殊工作。

Perhaps a better option is to set the value of CMAKE_CXX_FLAGS directly: 也许更好的选择是直接设置CMAKE_CXX_FLAGS的值:

cmake_minimum_required(VERSION 2.8)
set (CMAKE_CXX_FLAGS "-std=c++0x -stdlib=libc++ -g3 -Wall -O0")
add_executable(test test.cxx)

I saw this error when running gcc rather than g++ . 我在运行gcc而不是g++时看到了这个错误。 If I use g++ , things compile correctly. 如果我使用g++ ,事情就可以正确编译。 Perhaps your Makefile is using the wrong compiler front end. 也许您的Makefile使用了错误的编译器前端。

转到您的项目目标 - > Apple LLVM编译器4.1 - 语言 - >选择c ++库到

libc__(LLVM C++ stadard library with c++ 11 support);

After getting this error recently (using Clang) the problem was in not passing -std=c++11 -stdlib=libc++ as linker flags too. 最近收到此错误后(使用Clang)问题是没有将-std=c++11 -stdlib=libc++作为链接器标志传递。

So they have to be passed both as compiler flags and as linker flags. 因此,它们必须作为编译器标志链接器标志传递。

For GCC the first flag is -std=c++0x , instead of -std=c++11 . 对于GCC,第一个标志是-std=c++0x ,而不是-std=c++11

对我来说,答案只是使用clang++而不是clang

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM