cmake添加查找目录,如何使用CMake正确添加包含目录

2023-05-16

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 contains headers to be included, and that those headers should be tracked by the generated Makefile?

解决方案

Two things must be done.

First add the directory to be included:

target_include_directories(test PRIVATE ${YOUR_DIRECTORY})

In case you are stuck with a very old CMake version (2.8.10 or older) without support for target_include_directories, you can also use the legacy include_directories instead:

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.

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

target_include_directories(mylib PRIVATE ${YOUR_DIRECTORY})

add_executable(myexec execfile.cpp ${HEADER_FILES})

target_include_directories(myexec PRIVATE ${YOUR_DIRECTORY})

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

cmake添加查找目录,如何使用CMake正确添加包含目录 的相关文章

  • 永久关闭swap分区

    参考文章 xff1a https blog 51cto com 6923450605400 735323 xff08 1 xff09 临时关闭swap分区 重启失效 swapoff a xff08 2 xff09 永久关闭swap分区 se
  • querySelector() 方法

    返回文档中匹配指定 CSS 选择器的一个元素 虽然IE8中没有getElementsByClassName 但可以用querySelector 代替 注意 xff1a querySelector 方法仅仅返回匹配指定选择器的第一个元素 如果
  • 《Programming in Lua 3》读书笔记(二十五)

    日期 xff1a 2014 8 11 Part The C API 29 User Defined Types in C 在之前的例子里 xff0c 已经介绍过如果通过用C写函数来扩展Lua 在本章 xff0c 将会介绍通过用C写新的类型来
  • EntityFramework

    How to Call StoreProcedure http www tudou com programs view 0WtDy50Hbzs target 61 blank If it not work see http stackove
  • HOOK技术的一些简单总结

    好久没写博客了 xff0c 一个月一篇还是要尽量保证 xff0c 今天谈下Hook技术 在Window平台上开发任何稍微底层一点的东西 xff0c 基本上都是Hook满天飞 xff0c 普通应用程序如此 xff0c 安全软件更是如此 xff
  • windows2000上如何跑MSN

    WINDOWS2000上一直可以跑MSN的 LP公司还在用可怜的2000 xff0d xff0d 在9月13日以前 xff0c 我就一直这样与LP MSN联系 但之后就不行了 xff0c 直到今天 xff0c LP就没能再登录MSN 兹事体
  • 30岁人生进度条_30岁前,人生可以靠努力,30岁后,人生更需要一些“觉醒”

    慈怀女子 偶然看到胡歌一位粉丝的留言 xff0c 触动很深 入行15年 xff0c 如今的胡歌 xff0c 已活成了百万粉丝心中的 灯塔 24岁之前 xff0c 胡歌的青春几乎是开挂的 高中起就拍广告 xff0c 迅速在上海广告界蹿红 大二
  • win10环境安装TensorFlow2.0 - 吴恩达视频-修复版

    文章整体参考吴恩达老师的视频 xff0c 见如下 https www bilibili com video BV19E411Y7Kw p 61 4 实际结果 xff1a 不可行 xff01 版本安装后1 1 0 xff01 本文提供解决方案
  • tensorflow2.0入门与实战(笔记) 独热编码与交叉熵损失函数

    百度的独热编码定义 独热编码即 One Hot 编码 xff0c 又称一位有效编码 xff0c 其方法是使用N位状态寄存器来对N个状态进行编码 xff0c 每个状态都有它独立的寄存器位 xff0c 并且在任意时候 xff0c 其中只有一位有
  • 树莓派基础信息

    默认的用户名 pi 默认的密码是 raspberry 如果SSH连接别拒绝 xff0c 可能要修改以下密码 xff0c 使用命令sudo passwd pi aptitude可以解决安装包依赖问题
  • ESP32+arduino开发基础信息

    开发环境准备 官方的开发环境配置指导 xff1a Installing Arduino ESP32 2 0 2 documentation 实测可用 xff0c 其中在IDE首选项中增加的URL xff0c development和stab
  • git常用命令汇总

    Git本地操作 git init初始化git库git add添加文件git rm删除文件git commit m 34 xxxx 34 提交修改 xff0c 并增加提交日志git status查看版本库状态git log查看近期提交记录gi
  • 移植python3到ARM平台的OK6410开发板

    1 下载 python3 2 2 的源码 源码下载地址 xff1a http www python org ftp python 3 2 2 Python 3 2 2 tgz xff08 1 xff09 在 Ubuntu 上必须安装同版本的
  • 串口通信校验方式(even,odd,space,mark) 分类: 开...

    无校验 xff08 no parity xff09 奇校验 xff08 odd parity xff09 xff1a 如果字符数据位中 34 1 34 的数目是偶数 xff0c 校验位为 34 1 34 xff0c 如果 34 1 34 的
  • alias 命令的用法/相对路径和绝对路径/cd命令/rmdir,mkdir 创建和删除命令/rm命令...

    alias 命令的用法 相对路径和绝对路径 cd命令 rmdir xff0c mkdir 创建和删除命令 rm命令 alias是用做别名用 如下 xff1a root 64 alex alias alias cp 61 39 cp i 39
  • HTTP基础认证Basic Authentication

    HTTP基础认证Basic Authentication Basic Authentication是一种HTTP访问控制方式 xff0c 用于限制对网站资源的访问 这种方式不需要Cookie和Session xff0c 只需要客户端发起请求
  • c语言中结构体对齐详解

    为什么要对齐 现代计算机中内存空间都是按照byte划分的 xff0c 从理论上讲似乎对任何类型的变量的访问可以从任何地址开始 xff0c 但实际情况是在访问特定类型变量的时候经常在特 定的内存地址访问 xff0c 这就需要各种类型数据按照一
  • CAN总线基础和在linux下使用实战

    CAN总线基础和在linux下使用实战 CAN 是Controller Area Network 的缩写 有CANH和CANL两线 xff0c 即差分信号通信 当然设备芯片还会有电源和地等线 在总线空闲时 xff0c 所有的单元都可开始发送
  • C和C++的区别,有你不知道的

    c和c 43 43 可以说现在都是比较流行的 xff0c 但是两者到底有什么联系和区别吗 xff0c 这是学习c和c 43 43 最需要注意的 xff0c 不要把两者搞混了 xff0c 我们先开始就来看一下c和c 43 43 有什么联系 x
  • 详解摘要认证

    1 什么是摘要认证 摘要认证与基础认证的工作原理很相似 xff0c 用户先发出一个没有认证证书的请求 xff0c Web服务器回复一个带有WWW Authenticate头的响应 xff0c 指明访问所请求的资源需要证书 但是和基础认证发送

随机推荐