Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use CMake file (GLOB SRCS *. ) with a build directory

Tags:

this is my current CMakeLists.txt file

cmake_minimum_required(VERSION 3.3) set(CMAKE_C_FLAGS " -Wall -g ") project( bmi ) file( GLOB SRCS *.cpp *.h ) add_executable( bmi ${SRCS})  

This builds from my source directory, but I have to clean up all the extra files after. My question is how do I build this from a build directory if all my source files are in the same source directory?

thanks

like image 504
Will Meyers Avatar asked Jan 18 '16 20:01

Will Meyers


People also ask

What is GLOB CMake?

GLOB will generate a list of all files that match the globbing expressions and store it into the variable. Globbing expressions are similar to regular expressions, but much simpler. If RELATIVE flag is specified for an expression, the results will be returned as a relative path to the given path.

Should I use GLOB CMake?

The creators of CMake themselves advise not to use globbing. (We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists. txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.)

How do I upload a file to CMake?

First, you use include_directories() to tell CMake to add the directory as -I to the compilation command line. Second, you list the headers in your add_executable() or add_library() call.


2 Answers

If you really need to use file(GLOB …), this CMakeLists.txt should work :

cmake_minimum_required(VERSION 3.3) project(bmi) add_definitions("-Wall" "-g") include_directories(${PROJECT_SOURCE_DIR}) file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/*.cpp) add_executable(bmi ${SRC_FILES}) 

In this case you have to launch cmake from your build directory every time you add or delete a source file :

cmake <your_source_dir> -G <your_build_generator> 

As Phil reminds, CMake documentation doesn't recommend this use of GLOB. But there are some exceptions. You'll get more information on this post.

If you don't meet those exceptions, you'd rather list your source files than use GLOB :

set(SRC_FILES ${PROJECT_SOURCE_DIR}/main.cpp               ${PROJECT_SOURCE_DIR}/bmi.cpp               … ) 

NB : if you have #include of your .h files in .cpp files, I don't see any reason to put them in add_executable, you just need to specify include directory with include_directories.

like image 169
cromod Avatar answered Sep 29 '22 08:09

cromod


Cmake used to only update the list of source files if CMakeLists.txt was changed since the last cmake run or if cmake was used to configure the project again.

In cmake 3.11.0 recursive search and automatic re-configuration on adding or deleting source files was added. Since then you can use the following snippet:

if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.11.0")     file(GLOB_RECURSE SOURCE_FILES CONFIGURE_DEPENDS *.cpp *.h) else()     file(GLOB SOURCE_FILES *.cpp *.h */*.h */*.cpp) endif() 

The file() command after the else() provides at least a bit of backwards compatibility: It still searches for source files in the current folder and its direct subfolders. But it doesn't automatically recognize if there are new files or old files have been deleted.

Note that VERSION_GREATER_EQUAL is only available in cmake >= 3.7

like image 28
Gunter Königsmann Avatar answered Sep 29 '22 08:09

Gunter Königsmann