每次我添加新的源子文件夹时,Cmake重新编译所有内容 [英] Cmake recompiles everything each time I add a new source subfolder

查看:1580
本文介绍了每次我添加新的源子文件夹时,Cmake重新编译所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目树组织如下:

  MyProjects /  -  build  -  project1  -  CMakeLists.txt 
| | project2 - CMakeLists.txt
|
| src - project1 - Project1Class1.h
| Project1Class1.cpp
| Project1Class2.h
| Project1Class2.cpp
|更多子目录...
project2 - Project2Class1.h
| Project2Class1.cpp
|更多子目录...

想象project2依赖project1。然后project2直接使用project1文件,不使用静态或动态project1库。
然后,project2 / CMakeLists.txt找到project1和project2源文件,并通过GLOB_RECURSE:

 文件(
GLOB_RECURSE
source_files
../../project1/
../../project2/



这是在正确构建我的项目的意义上工作。



在新文件夹中添加新的源文件,例如在src / project2 /中输入文件MyNewFolder / myTest.cpp,然后输入

 〜/ MyProjects / build / project2 / $ cmake。 
〜/ MyProjects / build / project2 / $ make

帐户由cmake。



同样的事情,当我更改一个源文件在project1并尝试编译project2。



请注意,我大大简化了我的CMakeLists.txt的真正的。所以我的问题是:根据我解释,这是CMake期望做什么行为?如果是的,它的背后的理由是什么和我应该做什么 make 只编译新文件?我无法在互联网上找到任何有关它的文档。



注意:随意讨论整个源代码构建文件组织。注意,我想保持我的构建配置与src /文件夹分离。



编辑:我发现解释了为什么GLOB和GLOB_RECURSE阻止它工作。



编辑2:即使没有GLOB,编译也是从其他情况开始的(参见问题)

解决方案

文件(GLOB_RECURSE ...)的效果。我不知道为什么这正是发生,但为了避免这一点,大多数基于CMake的项目明确列出他们的来源:

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


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)

解决方案

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

这篇关于每次我添加新的源子文件夹时,Cmake重新编译所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆