How to use CMake file (GLOB SRCS *.) With build directory

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 comes from my source directory, but after that I have to clear all the extra files. My question is: how can I build this from the build directory if all the source files are in the same source directory?

thanks

+4
source share
2 answers

If you really need to use a 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 should run cmake from the build directory every time you add or remove the source file:

cmake <your_source_dir> -G <your_build_generator>

As Phil recalls, the CMake documentation does not recommend the use of GLOB. But there are some exceptions. You will receive more information about this message.

, , GLOB:

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

NB: #include .h .cpp , add_executable, include directory with include_directories.

+2

, . cmake:

. GLOB . CMakeLists.txt , , CMake.

0

Source: https://habr.com/ru/post/1624850/


All Articles