如何正确添加包含目录与CMake? [英] How to properly add include directories with CMake?

查看:766
本文介绍了如何正确添加包含目录与CMake?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大约一年前,我问过关于 CMake 中的标头依存关系。



我最近意识到,问题似乎是CMake认为这些头文件是外部项目。至少,当生成一个Code :: Blocks项目时,头文件不会出现在项目中(源文件)。因此,在我看来,CMake认为这些标头是外部项目,并不跟踪它们在依赖。



在CMake教程中搜索只指向 include_directories ,这似乎不能做我想做的...


向CMake发信号通知特定目录包含头文件,并且这些头文件应该由生成的Makefile跟踪的正确方法是什么?



解决方案

必须做两件事。



首先添加要包含的目录: / p>

  include_directories($ {YOUR_DIRECTORY})

然后您还必须将头文件添加到当前目标的源文件列表中,例如:

  set(SOURCES file.cpp file2.cpp $ {YOUR_DIRECTORY} /file1.h $ {YOUR_DIRECTORY} /file2.h)
add_executable(test $ {SOURCES})

这样,头文件将在Makefile中显示为依赖关系,例如在生成的visual studio工程中

如何将这些头文件用于多个目标

  set(HEADER_FILES $ {YOUR_DIRECTORY} /file1.h $ {YOUR_DIRECTORY} /file2.h)

add_library(mylib libsrc.cpp $ {HEADER_FILES})
add_executable(myexec execfile.cpp $ {HEADER_FILES})


About a year ago I asked about header dependencies in CMake.

I realized recently that the issue seemed to be that CMake considered those header files to be external to the project. At least, when generating a Code::Blocks project the header files do not appear within the project (the source files do). It therefore seems to me that CMake consider those headers to be external to the project, and does not track them in the depends.

A quick search in the CMake tutorial only pointed to include_directories which does not seem to do what I wish...

What is the proper way to signal to CMake that a particular directory contain headers to be included, and that those headers should be tracked by the Makefile generated ?

解决方案

Two things must be done.

First add the directory to be included:

include_directories(${YOUR_DIRECTORY})

Then you also must add the header files to the list of your source files for the current target, for instance:

set(SOURCES file.cpp file2.cpp ${YOUR_DIRECTORY}/file1.h ${YOUR_DIRECTORY}/file2.h)
add_executable(test ${SOURCES})

This way, the header files will appear as dependencies in the Makefile, and also for example in the generated visual studio project, if you generate one.

Edit: How to use those header files for several targets

set(HEADER_FILES ${YOUR_DIRECTORY}/file1.h ${YOUR_DIRECTORY}/file2.h)

add_library(mylib libsrc.cpp ${HEADER_FILES})
add_executable(myexec execfile.cpp ${HEADER_FILES})

这篇关于如何正确添加包含目录与CMake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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