vendredi 25 août 2017

C++11 with clang on macOS 10.12

I'm trying to use C++11 on macOS 10.12 with clang and cmake

Example program:

#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 file:

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)

Run cmake in terminal:

> 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

Run make in terminal:

> 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/http://ift.tt/1BXhDzK --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

I know the problem with the standard libraries. But the flag -stdlib=libc++ should be the right one for clang in macOS. If I use -stdlib=libstdc++ instead I get even worse errors (compiler errors not linker errors).

What is going wrong here? Did I miss something?

My example code can be found on http://ift.tt/2xjJf8H

Aucun commentaire:

Enregistrer un commentaire