更新CMake(似乎已经)破坏了我的程序

时间:2018-07-04 20:58:49

标签: c++ macos cmake linker-errors

我从CMake 3.6更新到了最新版本(3.12.0-rc2),现在我的程序之一无法编译。

奇怪的是,错误消息显示了标准库本身中的未定义符号。这是错误消息:

Undefined symbols for architecture x86_64:
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string(char const*)", referenced from:
      GetName() in ncc.cpp.o
      Expression() in ncc.cpp.o
      Term() in ncc.cpp.o
      GetNum() in ncc.cpp.o
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string()", referenced from:
      GetName() in ncc.cpp.o
      GetNum() in ncc.cpp.o
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::operator+=(char)", referenced from:
      GetName() in ncc.cpp.o
      GetNum() in ncc.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]: *** [ncc] Error 1
make[1]: *** [CMakeFiles/ncc.dir/all] Error 2
make: *** [all] Error 2

该如何解决?我需要链接到标准库的其他版本吗?如果是这样,我该如何告诉CMake?

这是我的CMakeLists.txt文件:

cmake_minimum_required(VERSION 3.0)

project(ncc)

add_definitions(-std=c++14)

add_subdirectory(${CMAKE_SOURCE_DIR}/src/)

add_executable(ncc ${CompSource})
add_executable(ncc-opt ${OptSource})
add_executable(nci ${IntSource})

我尝试了add_definitions行,没有它,似乎没有什么作用。

这是运行make VERBOSE=1的相关输出:

Scanning dependencies of target ncc
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/ncc.dir/build.make CMakeFiles/ncc.dir/build
[ 83%] Building CXX object CMakeFiles/ncc.dir/src/ncc.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk   -std=c++14 -o CMakeFiles/ncc.dir/src/ncc.cpp.o -c /Users/nepps/Dropbox/Projects/ncc/src/ncc.cpp
[100%] Linking CXX executable ncc
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/ncc.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++   -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/ncc.dir/src/ncc.cpp.o  -o ncc 
Undefined symbols for architecture x86_64:
(same errors as above)

如果需要更多信息,请随时发表评论。我正在使用运行MacOS 10.13.3的MacBook Pro。

1 个答案:

答案 0 :(得分:-1)

好吧,我不确定为什么它起作用,但是程序中的所有功能都标记为static,将其删除会使错误消失。走吧。