Updating CMake (seems to have) broke my program

2019-07-01 23:35发布

问题:

I updated from CMake 3.6 to whatever the latest version is (3.12.0-rc2) and now one of my programs won't compile.

The weird thing is, the error message shows undefined symbols from in the standard library itself. Here's the error message:

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

How do I fix this? Do I need to link to a different version of the standard library? And if so, how I tell CMake that?

Here's my CMakeLists.txt file:

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})

I tried it with the add_definitions line and without it, which didn't seem to make a difference.

This is the relevant output from running 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)

Feel free to comment if you need more information. I'm using a MacBook pro running MacOS 10.13.3.

回答1:

Well, I'm not sure why it worked, but all of the functions in the program were marked static and removing that made the errors go away. Go figure.