CMake入门教程:使用target_include_directories指定头文件所在文件夹

编程 专栏收录该内容
¥59.90 ¥99.00
480篇文章

在CMake中,使用target_include_directories命令可以指定项目中源代码文件所需的头文件所在的文件夹。该命令允许开发人员明确告知编译器在哪里查找特定的头文件。

下面将提供一个简单的示例,展示如何使用target_include_directories命令来包含指定文件夹中的头文件。

假设我们有一个项目,其中包含两个源代码文件:main.cpp和utility.cpp。main.cpp文件需要使用utility.cpp中的功能,因此需要包含utility.h头文件。

首先,我们需要在CMakeLists.txt文件中添加以下内容,指定头文件所在的文件夹:

# CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project(MyProject)

# 添加头文件所在的文件夹
target_include_directories(MyProject PUBLIC include)

在上述示例中,我们使用target_include_directories命令将include文件夹添加为头文件所在的文件夹。关键字PUBLIC用于指定这些头文件应该在项目中的所有目标中可见。

接下来,我们需要在源代码中使用这些头文件。假设我们的utility.h头文件位于include文件夹下,我们可以在main.cpp文件中添加以下内容:

// main.cpp

深入解析target_include_directories在CMake项目中的关键作用 本文深入解析CMake中target_include_directories命令的关键作用,详细阐述PRIVATE、PUBLIC和INTERFACE三种作用域的区别与应用场景。通过实战案例展示如何利用该命令精确管理头文件路径,实现模块化构建,避免依赖污染,从而打造清晰、健壮且易于维护的C/C++项目结构。 阅读详情

相关推荐

CMake中include_directories的实战解析:从基础配置到现代最佳实践

本文深入解析CMake中include_directories的实战应用,从基础配置到现代最佳实践。通过对比传统include_directories与现代target_include_directories的优劣,帮助开发者优化C++项目的头文件管理,提升编译效率与项目可维护性。

weixin_30432579的博客 322

C++项目头文件路径冲突:重名文件夹问题的根源与CMake工程化解决方案

在C++大型项目开发与跨团队协作中,头文件包含机制是构建系统的核心基础。编译器通过预处理器解析`#include`指令,在指定的搜索路径(`-I`)中查找头文件,这一过程的顺序和范围直接决定了最终使用的代码版本。当不同模块或第三方库中存在同名文件夹(如常见的`common`、`utils`)时,极易引发**头文件搜索路径冲突**,导致编译器误用错误版本的文件或产生重定义错误,严重影响编译成功率和运行时正确性。其技术价值在于保障模块隔离性与构建可靠性,是持续集成和微服务架构下的关键工程实践。应用场景广泛存在于

weixin_34389926的博客 453

Cmake构建工程设计——Cmake工程资源加载

加载文件夹:add_subdirectory 添加CMakeLists.txt层级下的子文件夹,cmake执行时会自动进入该文件夹。添加时要确保子文件夹也要有CMakeLists.txt文件。父目录的变量可以传递给子级目录。 添加该CMakeLists.txt文件目录下的src文件夹 add_subdirectory(src) 添加头文件 include_directories 该命令默认的作用域为该层级目录以及下属子目录。 由于父目录的变量可以传递给子级目录,头文件路径添加也遵循该规律,但子级目录的变量

zxng_work的博客 1025

CMake:target_include_directories

其中target是通过add_executable() 或add_library() 创建的,不能是通过ALIAS重命名的。m.c:2:10: fatal error: data.h: 没有那个文件或目录 #include "data.h"target_include_directories:指定目标在该路径下引入所依赖的头文件。加入target_include_directories后,程序可以正常的编译了。#设置在程序目录下的include目录中查找头文件。以下用一个例子进行说明该指令。

风静如云的博客 1537

CMake入门教程:使用target_include_directories指定头文件目录

在CMake中,可以使用target_include_directories命令来指定项目的头文件目录,使得编译器能够正确地找到这些头文件。总结起来,通过使用target_include_directories命令,我们可以在CMake中指定项目的头文件目录,使得编译器能够正确地找到这些头文件。在上述代码中,我们包括了iostream标准库以及我们自定义的头文件utils.h,并在main函数中调用了printMessage函数。其中,src目录存放我们的源代码文件,include目录存放我们的头文件。

TechPulseZ的博客 1374

CMake入门教程【基础篇】4.target_include_directories包含指定文件夹头文件

target_include_directories包含指定文件夹头文件

小鱼酱的博客 1415

学习SLAM:SLAM进阶(五)CMake基础篇---用cmake配置静态库、共享库、仅有头文件的库、对象库并使用这些库

用cmake配置静态库、共享库、仅有头文件的库、对象库并使用这些库

qq_41694024的博客 4768

手把手教你用C#和LibUsbDotNet实现USB设备通信(含完整代码示例)

本文详细介绍了如何使用C#和LibUsbDotNet库实现USB设备通信,包括设备识别、连接建立、同步/异步数据读写等核心操作。通过完整的代码示例,开发者可以快速掌握USB通信的关键技术,适用于工业自动化、医疗设备等多种场景。重点讲解了数据读写优化和错误处理技巧,帮助提升通信效率和可靠性。

a3b4c5的博客 707

别再为授权费头疼了!手把手教你免授权搞定西门子、马扎克等12种主流数控机床的数据采集

本文提供了一套免授权采集西门子、马扎克等12种主流数控机床数据的实战指南,帮助中小型机加工车间节省80%以上的采集成本。通过TCP/UDP协议逆向工程和跨平台数据整合,实现经济、灵活且可持续的数据采集方案,涵盖设备兼容性处理、数据频率优化等关键问题。

weixin_26873711的博客 709

CMAC算法原理

2、K1 通过以下运算导出:若 L 的最高有效位等于 0,则 K1 是 L 的左移 1 位。3、K2 通过如下运算导出:若 K1 的最高有效位等于 0,则 K2 是 K1 的左移 1 位。采用了 AES 算法中的类似 CBC 模式的加密方式,即对前一个分组的密文与当前分组的明文进行异或操作后在加密,2、分块处理:首先将消息分成多个固定长度的块,若消息长度不是块大小的倍数,则可以使用填充来将其填充到合适的大小。1、子密钥计算,通过主密钥生成子密钥(key1,key2),子密钥用于最后一个块处理;

sky的博客 1659

Clang Static Analyzer 使用教程:本地 + CMake + GitHub Actions 自动静态分析实战

本文介绍了ClangStaticAnalyzer静态代码分析工具的使用方法。该工具基于Clang/LLVM,可检测C/C++代码中的内存泄漏、空指针等常见错误。主要内容包括:安装指南(Ubuntu/macOS/Windows)、命令行使用方法、CMake项目集成方案、GitHub Actions自动化分析配置,以及HTML报告生成与查看。文章还提供了官方资源链接,帮助开发者系统掌握从本地调试到持续集成的完整静态分析工作流,提升代码质量保障能力。适用于需要在开发过程中实施静态分析的C/C++项目。

u014358031的博客 2159

CAN-do-paser:Java开发的CAN总线数据帧解析库

CAN总线数据帧使用DBC文件信息解析为物理量,基于Java开发的数据解析工具。

佳瑞的博客 1136

ZYNQ PS裸机开发RS485

RS485 是一种通用的通信标准,广泛用于数据采集和控制应用中。它的主要优点之一是它允许将多个 RS485 设备放在同一条总线上,这使得多个节点可以相互连接。RS-485是美国电子工业协会(EIA)在1983年批准了一个新的平衡传输标准(balanced transmission standard),EIA一开始将RS(Recommended Standard)做为标准的前缀,不过后来为了便于识别标准的来源,已将RS改为EIA/TIA。

baidu_34971492的博客 831

Claude Code 100个真实案例 - 用AI设计Arduino PCB电路板(硬件小白也能画板)

本案例展示如何使用 Claude Code 配合 KiCad 的 Python 脚本接口,从零设计一块 Arduino 传感器扩展板 PCB。从原理图符号创建、网表生成到 PCB 布局布线,全程自动化完成,最终输出可直接发给工厂打样的 Gerber 文件。

yueyeguzhuo的专栏 446

锐捷 MPLS VPN 配置

ISP内部配置 , PE设备和CE设备之间的接口不开启 ( 只在物理接口上配置 , loopback 口不配置 )最后一步关闭 ipv4 单播 , 如果你发现路由奇奇怪怪的 , 可以尝试一下。both --> 同时配置 import export 值。绑定后接口会重置 , 且接口的路由会转到VRF路由表中。这里基本上绑定了 vrf 实例的接口都需要宣告进去。vpnv 4 通道是用来传递 mpls 路由的。先建立邻居管理,建立VPNV4通道。这里要做双向引入 , 才会有路由。export 导出值。

dekangzou的博客 1712

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

上一篇: 使用BOOST.ASSERT机制替换库断言
下一篇: 使用C++ Boost库中的type_index模块进行constexpr相关的测试程序
普通网友
博客等级 码龄3年 107粉丝 680原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值