我正在使用CMake 3.8.2,GNU make 4.2.1和GCC 6.4.0进行我的C ++ 14项目,我注意到构建时出现了一种奇怪的行为。我在一个名为" build"的子文件夹中使用CMake进行源外构建。我在哪里运行cmake ..
后跟make
。
CMake运行良好,没有任何错误,make将构建所有源文件,就像我预期的那样,直到它完成编译并开始链接它们。然后它将失败并显示错误
[ 83%] ...
[100%] Linking CXX executable myproject
/usr/bin/ld: some-source-file.cc.o: undefined reference to symbol '_ZNKSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEE3strEv@@GLIBCXX_3.4.21'
有趣的是,到目前为止它没有显示任何编译器警告,只显示上面提到的链接器错误。
现在,当我忽略错误并简单地再次运行cmake ..
然后再运行make
时(就像我之前一样),我得到了我的代码应该生成的所有编译器警告,并且所有链接都完美无缺,甚至虽然我在此期间没有改变任何代码或CMake相关文件。
我可以通过运行build
删除rm -r *
目录中的所有文件来重现此行为。
这是我的CMakeLists.txt
文件:
# Define minimum required CMake version
cmake_minimum_required(VERSION 3.8.2)
# Setting compiler related settings
set(CMAKE_CXX_COMPILER "${CMAKE_SOURCE_DIR}/toolchain/binary/gcc-6.4.0/bin/gcc")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wconversion -O2 -lstdc++")
set(CMAKE_CXX_STANDARD 14)
# Define project name
project(MyProject)
# Find source files
file(GLOB_RECURSE SOURCES application/*.cc)
# Adding third-party sources
set(SOURCES ${SOURCES} "third-party/cpp-base64/base64.cpp")
# Executable to be built from which source files
add_executable(myproject ${SOURCES})
# Find and include and link Botan
find_library(BOTAN botan-2 "third-party/botan/build/lib")
include_directories("third-party/botan/build/include/botan-2")
# Includes that are part of the project
include_directories("${CMAKE_SOURCE_DIR}/application/include")
# Include nlohmann/json
include_directories("third-party/json/src")
# Include cpp-base64 by René Nyffenegger
include_directories("third-party/cpp-base64")
find_package(Boost REQUIRED COMPONENTS program_options)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
endif()
# Link third-party libraries
target_link_libraries(myproject ${Boost_LIBRARIES} ${BOTAN})
注意:我需要签入我正在使用的编译器和库,这就是我在CMake文件中指定它们的原因。
答案 0 :(得分:3)
如果它仅在第二次有效时才与缓存变量有关。
所以我非常确定如果您通过添加CMAKE_CXX_COMPILER
来修改set(... CACHE INTERNAL ""
设置,它将首次运行:
set(CMAKE_CXX_COMPILER "${CMAKE_SOURCE_DIR}/toolchain/binary/gcc-6.4.0/bin/gcc" CACHE INTERNAL "")
在set(CMAKE_CXX_FLAGS ...)
命令后移动project()
。
但请注意,您不应将编译器放入CMakeLists.txt
。
<强>参考强>