简体   繁体   English

cmake 使用 *.cpp 规则创建 Makefile

[英]cmake to create Makefile with *.cpp rule

I'm just getting started with cmake, I've read some tutorials and got the basic stuff working.我刚刚开始使用 cmake,我已经阅读了一些教程并开始使用基本的东西。

However currently my CMakesList.txt generates a Makefile that has every .cpp explicitly named.但是,目前我的 CMakesList.txt 生成一个 Makefile,其中每个 .cpp 都显式命名。 That means, whenever I add a new .cpp to my project, I need to rerun cmake, before I can make.这意味着,每当我向项目添加新的 .cpp 时,我都需要重新运行 cmake,然后才能进行制作。 While this is not a huge deal, it's still a bit annoying.虽然这不是什么大问题,但它仍然有点烦人。

Since Makefiles can do something like SOURCES=*.cpp , I thought there's probably a way to tell cmake to generate the Makefile with such a rule!?由于 Makefiles 可以做类似SOURCES=*.cpp事情,我想可能有办法告诉 cmake 用这样的规则生成 Makefile!?

I know I can do我知道我能做到

cmake_minimum_required(VERSION 2.8)
file(GLOB SRC
    "*.h"
    "*.cpp"
)
add_executable(main ${SRC})

but with that I still have to rerun cmake.但是我仍然需要重新运行cmake。

As far as I know, the way to do it is as you have said and the downside is that you have to run cmake every time you add a file. 据我所知,这样做的方法就像你说的那样,缺点是每次添加文件时都必须运行cmake。

What I usually do to overcome this is to make a custom target that calls cmake as well. 我通常要做的就是制作一个调用cmake的自定义目标。 It goes like this 它是这样的

ADD_CUSTOM_TARGET(update
    COMMAND ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR}
    COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
    COMMENT "Cmake and build"
)

With this you may call make update and it will call cmake first which captures all the files and then starts the build process. 有了这个,您可以调用make update ,它将首先调用cmake捕获所有文件,然后启动构建过程。

Cmake must be re-runned only if you change your files, becaues CMake compiles the CMakeLists.txt once and then it generates a cached intermediary format that is much more performant. 只有在您更改文件时才必须重新运行Cmake,因为CMake编译CMakeLists.txt一次,然后生成一个更高效的缓存中间格式。 (If you don't add any new file, it is fine bu just running the make command). (如果你没有添加任何新文件,那么运行make命令就好了)。

Note that CMake cannot use the equivalent feature of MakeFile because otherwise it would f*** up all file dependencies. 请注意,CMake不能使用MakeFile的等效功能,否则会导致所有文件依赖性。 CMake is not designed to work only with Makefiles as such it only uses a subset of features of target build scripts. CMake不能仅用于Makefile,因为它只使用目标构建脚本的一部分功能。

Also if you are already using CMake directly, there's no longer the need to generate makefiles, use CMake, and produce makefiles only if you need to ship your project to clients that have Makefile but not CMake. 此外,如果您已经直接使用CMake,则不再需要生成makefile,使用CMake,并且只有在需要将项目发送到具有Makefile但不是CMake的客户端时才生成makefile。

You can use this : 你可以用这个:

file( GLOB_RECURSE
      source_files
      ./*
)

add_executable(main ${source_files})

It is preferable to put all cpp and h files in a src directory then : 最好将所有cpp和h文件放在src目录中,然后:

file( GLOB_RECURSE
      source_files
      src/*
)

add_executable(main ${source_files})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM