Cmake recompiles everything each time I add a new source subfolder

断了今生、忘了曾经 提交于 2019-12-13 15:05:46

问题


I have a project tree organized as the following:

MyProjects/ - build - project1 - CMakeLists.txt
            |       | project2 - CMakeLists.txt
            |
            | src - project1 - Project1Class1.h
                             | Project1Class1.cpp
                             | Project1Class2.h
                             | Project1Class2.cpp
                             | more subdirectories ...
                    project2 - Project2Class1.h
                             | Project2Class1.cpp
                             | more subdirectories ...

Imagine that project2 depends on project1. Then project2 uses directly project1 files and does not use a static or dynamic project1 library. Then project2/CMakeLists.txt finds out the project1 and project2 source files and includes them through a GLOB_RECURSE :

file(
    GLOB_RECURSE
    source_files
    ../../project1/
    ../../project2/
)

This is working in the sense that it correctly builds my projects.

Each time I add a new source file in a new folder, in e.g. file MyNewFolder/myTest.cpp in src/project2/, and type

~/MyProjects/build/project2/$  cmake .
~/MyProjects/build/project2/$  make

Then the file is correctly taken into account by cmake. However, my problem is that every file is recompiled again.

Same thing when I change a source file in project1 and try to compile project2.

Note that I considerably simplified what really is in my CMakeLists.txt. So my question is: based on what I explained about it, is this behavior what CMake is expected to do? And if yes what is the rationale behind it and what should I do for make to only compile the new file? I was not able to find any documentation about it on the internet.

Note: Feel free to discuss the overall source build files organization. Notice that I wanted to keep my build configuration separated from the src/ folder.

Edit: I found this which explains why GLOB and GLOB_RECURSE prevent it to work.

Edit 2: Even with no GLOB, the compilation is done from the begining is other cases (see this question)


回答1:


You are observing known side effect of file(GLOB_RECURSE ...). I'm not sure why exactly this is happening, but to avoid this most CMake-based projects lists their sources explicitly:

set(source_files
  ../../project1/Project1Class1.cpp
  ../../project1/Project1Class2.cpp
  ...
  ../../project2/Project2Class1.cpp
  ../../project2/Project2Class1.cpp
  ...
)


来源:https://stackoverflow.com/questions/28521613/cmake-recompiles-everything-each-time-i-add-a-new-source-subfolder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!