如何在构建目录中使用CMake文件(GLOB SRCS *。) [英] how to use CMake file (GLOB SRCS *. ) with a build directory

查看:1275
本文介绍了如何在构建目录中使用CMake文件(GLOB SRCS *。)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前的CMakeLists.txt文件

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?

谢谢

推荐答案

如果您确实需要使用file(GLOB …),此CMakeLists.txt应该可以正常工作:

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})

在这种情况下,每次添加或删除文件时,都必须从构建目录中启动cmake。源文件:

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>

正如Phil提醒的那样,CMake文档不建议这种GLOB用法。但是也有一些例外。您将在此帖子中获得更多信息。

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.

如果您没有遇到这些例外,则宁愿列出您的源文件,也不愿使用GLOB:

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:如果您将.h文件包含在.cpp文件中,那么我看不出有任何理由将它们放入add_executable中,需要使用include_directories指定包含目录。

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.

这篇关于如何在构建目录中使用CMake文件(GLOB SRCS *。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆