Bérenger
Bérenger

Reputation: 2758

Cmake recompiles everything each time I add a new source subfolder

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)

Upvotes: 3

Views: 1537

Answers (1)

arrowd
arrowd

Reputation: 34411

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
  ...
)

Upvotes: 3

Related Questions