4

I'm just getting started with cmake, I've read some tutorials and got the basic stuff working.

However currently my CMakesList.txt generates a Makefile that has every .cpp explicitly named. That means, whenever I add a new .cpp to my project, I need to rerun cmake, before I can make. 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!?

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.

Johannes Stricker
  • 1,701
  • 13
  • 23

2 Answers2

5

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.

What I usually do to overcome this is to make a custom target that calls cmake as well. 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.

Ashkan
  • 1,050
  • 15
  • 32
  • 1
    Thanks, somewhat of a workaround, but still a nice solution! – Johannes Stricker May 27 '16 at 14:33
  • You're welcome. This is the best I could come up for my own usage. This is actually quite nice. Say for instance if you want to switch between debug and release, you could do the same thing by adding two custom targets. – Ashkan May 27 '16 at 14:37
  • There is no need to add a specific target, you can use `make rebuild_cache` to trigger the cmake call. – Bernhard Mar 26 '19 at 10:59
3

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. (If you don't add any new file, it is fine bu just running the make command).

Note that CMake cannot use the equivalent feature of MakeFile because otherwise it would f*** up all file dependencies. CMake is not designed to work only with Makefiles as such it only uses a subset of features of target build scripts.

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.

CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69
  • Okay, so what I want to do is not possible, right? I'm a bit confused though, I thought cmake would generate the Makefile and then I would still have to build using that file. You are saying I can use cmake directly to build the project? – Johannes Stricker May 27 '16 at 14:32
  • You can generate the Makefile, but the make file does not stay updated auto-magically, you need to regenerate the makefile if something important changes (Cmake script changes, or files are added into a GLOB).. Of course the "project" generation is for compability, you can just use Cmake directly with the `--build` directive – CoffeDeveloper May 27 '16 at 14:35
  • 1
    Okay, thanks. Then I can just have SublimeText use cmake --build instead of mingw32-make, I guess! – Johannes Stricker May 27 '16 at 14:46