在macOS 10.12上使用clang的C ++ 11

时间:2017-08-25 18:29:39

标签: macos c++11 clang++

我正在尝试使用clang和cmake

在macOS 10.12上使用C ++ 11

示例程序:

#include <vector>
#include <functional>

int main(void) {
  std::vector<std::string> str_values = {"abc", "bde"};
  auto count2 = std::count_if(str_values.begin(), str_values.end(),
                std::mem_fn(&std::string::empty));
  (void)count2;
  return 0;
}

CMakeLists.txt文件:

cmake_minimum_required (VERSION 3.1.0)
project (MacosC++11)

set(CMAKE_VERBOSE_MAKEFILE ON)

add_compile_options(-std=c++11)
add_compile_options(-stdlib=libc++)
add_executable(Test main.cpp)

在终端中运行cmake:

> cmake ../
-- The C compiler identification is AppleClang 8.1.0.8020042
-- The CXX compiler identification is AppleClang 8.1.0.8020042
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/bla/Development/MacosC++11/build

在终端中运行make:

> make
/usr/local/Cellar/cmake/3.9.1/bin/cmake -H/Users/bla/Development/MacosC++11 -B/Users/bla/Development/MacosC++11/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.9.1/bin/cmake -E cmake_progress_start /Users/bla/Development/MacosC++11/build/CMakeFiles /Users/bla/Development/MacosC++11/build/CMakeFiles/progress.marks
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/Makefile2 all
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/Test.dir/build.make CMakeFiles/Test.dir/depend
cd /Users/bla/Development/MacosC++11/build && /usr/local/Cellar/cmake/3.9.1/bin/cmake -E cmake_depends "Unix Makefiles" /Users/bla/Development/MacosC++11 /Users/bla/Development/MacosC++11 /Users/bla/Development/MacosC++11/build /Users/bla/Development/MacosC++11/build /Users/bla/Development/MacosC++11/build/CMakeFiles/Test.dir/DependInfo.cmake --color=
Scanning dependencies of target Test
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/Test.dir/build.make CMakeFiles/Test.dir/build
[ 50%] Building CXX object CMakeFiles/Test.dir/main.cpp.o
/Library/Developer/CommandLineTools/usr/bin/c++    -std=c++11 -stdlib=libc++ -o CMakeFiles/Test.dir/main.cpp.o -c /Users/bla/Development/MacosC++11/main.cpp
[100%] Linking CXX executable Test
/usr/local/Cellar/cmake/3.9.1/bin/cmake -E cmake_link_script CMakeFiles/Test.dir/link.txt --verbose=1
/Library/Developer/CommandLineTools/usr/bin/c++   -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/Test.dir/main.cpp.o  -o Test
Undefined symbols for architecture x86_64:
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::empty() const", referenced from:
      _main in main.cpp.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

我知道标准库的问题。但是标志-stdlib=libc++应该是macOS中clang的正确标志。如果我使用-stdlib=libstdc++而不是更糟糕的错误(编译器错误而不是链接器错误)。

这里出了什么问题?我错过了什么吗?

我的示例代码可以在https://github.com/LukasWoodtli/MacosC-11

上找到

0 个答案:

没有答案