CMake递归搜索头文件和源文件 [英] Recursive CMake search for header and source files

查看:1829
本文介绍了CMake递归搜索头文件和源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CMake的新手,想问一下有人可以帮助解决以下问题吗?

I am new to CMake and would like to ask if somebody can help in the following problem.

我在各自的文件夹中有C ++源文件和头文件,现在,我想制作一个CMake文本文件来递归搜索它们。

I have C++ source and header files in their respective folders and now, I want to make a CMake text file that recursively searches for them.

当前,我是按照这种方式执行的:

Currently, I am doing it in this way:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(CarDetectorDAISY)

file(GLOB_RECURSE SRCS *.cpp)
file(GLOB_RECURSE HDRS *.h)

ADD_EXECUTABLE(stereo_framework  ${SRCS} ${HDRS})
TARGET_LINK_LIBRARIES(stereo_framework) 

CarDetectorDAISY.sln 解决方案文件,当我尝试构建它时,它显示
的错误是找不到头文件(没有此类文件或目录)。

This creates my CarDetectorDAISY.sln solution file and when I try to build it, it shows an error that header files are not found (No such file or directory).

如果有人可以帮助我,我将不胜感激。谢谢。

It would be really grateful if someone can please help me. Thanks.

推荐答案

您可能缺少一个或多个 include_directories 通话。在 add_executable 调用中将标头添加到文件列表中实际上并没有添加标头,然后才将其添加到编译器的搜索路径中-这是一种方便的功能,即仅将标头添加到项目的文件夹结构中

You're probably missing one or more include_directories calls. Adding headers to the list of files in the add_executable call doesn't actually add then to the compiler's search path - it's a convenience feature whereby they are only added to the project's folder structure in IDEs.

因此,在您的根目录中,说您拥有/my_lib/foo.h,并且您希望将其包含在源文件中作为

So, in your root, say you have /my_lib/foo.h, and you want to include that in a source file as

#include "my_lib/foo.h"

然后在CMakeLists.txt中,需要执行以下操作:

Then in your CMakeLists.txt, you need to do:

include_directories(${CMAKE_SOURCE_DIR})

如果是,您只是想做

#include "foo.h"

然后在CMakeLists.txt,

then in the CMakeLists.txt, do

include_directories(${CMAKE_SOURCE_DIR}/my_lib)



我应该提到 file(GLOB ...) 不是收集源列表的推荐方法-实际上,您应该只在CMakeLists中显式添加每个文件.txt。这样,如果您以后添加或删除源文件,则CMakeLists.txt会被修改,并且在下次尝试构建时CMake会自动重新运行。 file


我们不建议使用GLOB从源代码树中收集源文件列表。如果没有CMakeLists.txt添加或删除源文件时文件发生更改,然后生成的生成系统无法知道何时要求CMake重新生成。

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.

这篇关于CMake递归搜索头文件和源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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