我在C++14项目中使用了GNU3.8.2、GNU 4.2.1和GCC 6.4.0,在构建时我注意到了一种奇怪的行为。我在一个名为" 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 * dir中的所有文件来再现这种行为。
这是我的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文件中指定它们的原因。
发布于 2017-12-07 11:19:12
如果它只在第二次工作,那么它与缓存的变量有关。
因此,我非常肯定,如果您第一次通过添加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中。
参考资料
https://stackoverflow.com/questions/47684410
复制相似问题