簡體   English   中英

CMake和Make需要運行兩次才能成功構建代碼

[英]CMake and Make need to be run twice in order to build code successfully

我在我的C ++ 14項目中使用CMake 3.8.2,GNU make 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相關文件。

我可以通過運行rm -r *刪除build目錄中的所有文件來重現此行為。

這是我的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文件中指定它們的原因。

如果它僅在第二次工作時與緩存變量有關。

所以我很確定如果你通過添加set(... CACHE INTERNAL "" )來修改CMAKE_CXX_COMPILER設置,它將會第一次運行:

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

並在project()命令之后移動set(CMAKE_CXX_FLAGS ...)

但請注意,您不應將編譯器放入CMakeLists.txt

參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM