2

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.

Dovahkiin
  • 946
  • 14
  • 25
  • Maybe the default c++ standard version has changed, are you selecting explicitely a version of the c++ standard in the CMakeFile, or are you letting CMake chose one? (please include the CMakeFile) – Olivier Sohn Jul 04 '18 at 21:32
  • I don't think that's the issue, as I tried with and without the `-std=c++1y` line. @OlivierSohn – Dovahkiin Jul 05 '18 at 03:26
  • Use a verbose build option, find out what commands actually run at compilation and link stages, and proceed from there. – n. m. could be an AI Jul 05 '18 at 04:01
  • 2
    @Dovahkiin according to https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html#index-std-108 `c++1y` is deprecated, you should probably use `c++14` instead – Olivier Sohn Jul 05 '18 at 07:04
  • Okay, I tried it, but it didn't change the error. @OlivierSohn – Dovahkiin Jul 06 '18 at 12:08
  • Maybe some compilation unit (or some library you link with) has seen headers from a different version of the C++ (there were changes on basic_string in c++17, this is what makes me think about that). – Olivier Sohn Jul 06 '18 at 13:42
  • https://stackoverflow.com/questions/27508451/cmake-error-undefined-symbols-for-x86-64 – sailfish009 Jul 08 '18 at 02:43
  • That doesn't really apply to this problem... @sailfish009 – Dovahkiin Jul 09 '18 at 00:52
  • @Dovahkiin good to know. – sailfish009 Jul 09 '18 at 02:05
  • What would help here is a minimal reproducible example. Also, maybe a bit naively, have you removed all (temporary) build files of the porject after updating? – Tom de Geus Jul 14 '18 at 12:23

1 Answers1

-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.

Dovahkiin
  • 946
  • 14
  • 25