cmake 多次编译_CMake和Make需要运行两次才能成功构建代码

I am using CMake 3.8.2, GNU make 4.2.1 and GCC 6.4.0 for my C++14 project and I noticed a strange behavior when building. I am using CMake for an out-of-source build in a sub-folder called "build" where I run cmake .. followed by make.

CMake runs fine without any errors and make will build all source files like I expect until it is done compiling and starts linking them. It will then fail with an error

[ 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'

Interestingly it doesn't show any compiler warnings up to this point and only shows the above mentioned linker error.

Now when I ignore the error and simply run cmake .. and then make again (just like I did before) I get all the compiler warnings that my code should produce and everything links perfectly fine, even though I didn't change any code or CMake-related files in the meantime.

I can reproduce this behavior by deleting all files in the build dir by running rm -r *.

Here is my CMakeLists.txt file:

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

Note: I am required to check-in the compiler and libraries I am using, which is why I specified them in the CMake file.

解决方案

If it only works the second time it has to do with cached variables.

So I'm pretty sure that it will work the first time if you modify CMAKE_CXX_COMPILER setting by adding set(... CACHE INTERNAL "") to:

set(CMAKE_CXX_COMPILER "${CMAKE_SOURCE_DIR}/toolchain/binary/gcc-6.4.0/bin/gcc" CACHE INTERNAL "")

And move set(CMAKE_CXX_FLAGS ...) after the project() command.

But please also be noted that you shouldn't put the compiler into your CMakeLists.txt.

References

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值