Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 611
Johannes Stricker Avatar asked Feb 07 '23 22:02

Johannes Stricker


1 Answers

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.

like image 70
Ashkan Avatar answered Feb 09 '23 11:02

Ashkan