每次在include_directory()中添加新目录时,Cmake都会重新编译所有内容

时间:2015-02-22 06:16:18

标签: build cmake build-process

(这个问题与this one I asked非常相似,但与GLOB的细微差别无关。因此,我认为将这两个问题分开会更清晰。

每次在我的CMakeLists.txt中添加或删除include_directory()(甚至为空)中的文件夹,然后输入

cmake .
make

触发整个项目重新编译。为什么以及我该怎么办呢?

我的实际CMakeLists.txt(注意没有GLOB):

cmake_minimum_required(VERSION 2.8)
cmake_policy(VERSION 2.8.12.2)

# Project
get_filename_component(ProjectName MyProject NAME)
project(${ProjectName})

# Compiler and options
set(CMAKE_CXX_COMPILER /usr/bin/clang++)
set(CLANG_COMPILE_FLAGS "-std=c++1y -stdlib=libc++ ")

# Type of build
set(CMAKE_BUILD_TYPE "Release")
# Build tool 
set(CMAKE_GENERATOR "Unix Makefiles")

set(EXECUTABLE_OUTPUT_PATH ../bin/${CMAKE_BUILD_TYPE})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CLANG_COMPILE_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CLANG_LINK_FLAGS}")

include_directories(
    [all folders containing .h files here] # adding anything here triggers the whole project to recompile
)

add_executable(
    ${ProjectName}
    [all .cpp and .h files here]
)

1 个答案:

答案 0 :(得分:2)

命令include_directories添加编译器标志(例如-I/path/to/include/dir的{​​{1}})。因此,如果您更改gcc =>编译器标志change =>目标需要重建。

实施例

为避免定期重新编译整个项目,您不需要经常修改include_directories。例如,您可以组织这样的代码:

include_directories

CMakeLists.txt的内容:

CMakeLists.txt
Source/
        - A/foo.hpp
        - A/foo.cpp
        - B/boo.hpp
        - B/boo.cpp

C ++文件的内容:

include_directories(./Source)
add_executable(myexe A/foo.hpp A/foo.cpp B/boo.hpp B/boo.cpp)