cmake的使用--目标的编译附源码
building simple targets
代码仓库地址
https://github.com/zzu-andrew/linux-sys/tree/dfew/CMake
Executables
If an executable is defined with the EXCLUDE_FROM_ALL option, it will not be included in that default ALL target.
add_executable(targetName [WIN32] [MACOSX_BUNDLE][EXCLUDE_FROM_ALL]source1 [source2 ...]
)
EXCLUDE_FROM_ALL
设置之后将不再生成该可执行程序
Defining Libraries
add_library(targetName [STATIC | SHARED | MODULE][EXCLUDE_FROM_ALL]source1 [source2 ...]
)
STATIC
生成静态库
SHARED
生成动态库
MODULE
windows
上的DLL
要是在add_library
中省略了动态库还是静态库的选项,可以在调用cmake
的时候通过编译选项指定如
cmake -DBUILD_SHARED_LIBS=YES /path/to/source
等价于在CMakeLists.txt
中设置
set(BUILD_SHARED_LIBS YES)
Linking Targets
CMake
captures this richer set of dependency relationships with its target_link_libraries() command, not just the simplistic idea of needing to link. The general form of the command is:
target_link_libraries(targetName<PRIVATE|PUBLIC|INTERFACE> item1 [item2 ...][<PRIVATE|PUBLIC|INTERFACE> item3 [item4 ...]]...
)
PRIVATE
Private dependencies specify that library A uses library B in its own internal implementation. Anything else that links to library A doesn’t need to know about B because it is an internal implementation detail of A.
PUBLIC
Public dependencies specify that not only does library A use library B internally, it also uses B in its interface. This means that A cannot be used without B, so anything that uses A will also have a direct dependency on B. An example of this would be a function defined in library A which has at least one parameter of a type defined and implemented in library B, so code cannot call the function from A without providing a parameter whose type comes from B.
INTERFACE
Interface dependencies specify that in order to use library A, parts of library B must also be 17used.
This differs from a public dependency in that library A doesn’t require B internally, it only uses B in its interface. An example of where this is useful is when working with library targets defined using the INTERFACE form of add_library(), such as when using a target to represent a header-only library’s dependencies
target_link_libraries
允许定义一个库是怎样依赖其他库,按照下面的顺序定义进行定义
add_library(collector src1.cpp)
add_library(algo src2.cpp)
add_library(engine src3.cpp)
add_library(ui src4.cpp)
add_executable(myApp main.cpp)
target_link_libraries(collector PUBLIC uiPRIVATE algo engine
)
target_link_libraries(myApp PRIVATE collector)
In this example, the ui
library is linked to the collector library as PUBLIC, so even though myApp
only directly links to collector, myApp
will also be linked to ui
because of that PUBLIC relationship。
the targetName
used with target_link_libraries() must have been defined by an add_executable() or add_library() command in the same directory from which target_link_libraries() is being called.
Linking Non-targets
target_link_libraries()
command is more flexible than that. In addition to CMake
targets, the following things can also be specified as items in a target_link_libraries() command:
Full path to a library file
CMake
will add the library file to the linker command. If the library file changes, CMake
will detect that change and re-link the target. prior 3.3 version (e.g. replace /usr/lib/libfoo.so with -lfoo
)CMake
会去查找库,带来的问题就是有大量历史缓存。
Plain library name
If just the name of the library is given with no path, the linker command will search for that library (e.g. foo becomes -lfoo or foo.lib
, depending on the platform). This would be common for libraries provided by the system.
Link flag
As a special case, items starting with a hyphen other than -l or -framework will be treated as flags to be added to the linker command. Cmake
手册中禁止这样使用,因为当是PUBLIC
或者INTERFACE
连接的时候这些命令可能会传递给其他的目标。
TIPS
目标名字和工程名字不必必须一样,当时通常教程和示例中会通过变量的形式将两者设置成一样的。
# Poor practice, but very common
set(projectName MyExample)
project(${projectName})
add_executable(${projectName} ...)
本章源码:
#This should be the first line of the CMakeLists.txtcmake_minimum_required(VERSION 3.2)# Poor practice, but very common
set(projectName MyProject)
project(${projectName} VERSION 4.7.2 LANGUAGES C)add_library(collector src1.c)
add_library(echo_demo echo_demo.c)#add_executable(targetName [WIN32] [MACOSX_BUNDLE]
# [EXCLUDE_FROM_ALL]
# source1 [source2 ...]
#)
# EXCLUDE_FROM_ALL 不对该可执行程序进行编译
set(executeProcess myExe) # 使用变量
add_executable(${executeProcess} main.c)# PUBLIC 的链接方式可以直接在链接echo_demo的目标中使用 collector库中的函数
target_link_libraries(echo_demoPUBLIC collector)target_link_libraries(${executeProcess}PRIVATE echo_demo
)
echo_demo.c
#include <stdio.h>int echo_demo(void)
{printf("echo demo\n");return 0;
}
#include <stdio.h>int echo_demo(void);int main(int argc, char const *argv[])
{int ret = 0;ret = echo_demo();if(ret != 0){printf("call echo demo failed\n");}return 0;
}
src1.c
#include <stdio.h>void src1(void)
{printf("src1, is Linked.\n");return;
}
cmake的使用--目标的编译附源码相关推荐
- Matlab之基于MTI雷达生成表面杂波和目标回波(附源码)
目录 一.MTI 雷达基础知识 1.1 MTI 过滤器 1.2 平台几何形状 1.3 性能指标 二.宽边 MTI 仿真 2.1 场景配置 2.2 配置雷达 2.3 配置方案 2.4 配置平台 2.5 ...
- CMake编译Nginx源码
背景 最近打算学习nginx源码,但使用clion IDE查看不支持跳转.因为源码是使用autotool维护的,而clion需要CMake管理项目.着手编译nginx源码. 环境 os : ubunt ...
- CVPR21最佳检测:不再是方方正正的目标检测输出(附源码)
计算机视觉研究院专栏 作者:Edison_G 有些目标往往具有任意方向的分布.因此,检测器需要更多的参数来编码方向信息,这往往是高度冗余和低效的... 公众号ID|ComputerVisionGzq ...
- Ubuntu12.04编译Android4.0.1源码全过程-----附wubi安装ubuntu编译android源码硬盘空间不够的问题解决
Ubuntu12.04编译Android4.0.1源码全过程-----附wubi安装ubuntu编译android源码硬盘空间不够的问题解决 参考文章: (1)Ubuntu12.04编译Android ...
- Spark 3.0 发布了,代码拉过来,打个包,跑起来!| 附源码编译
作者 | 敏叔V587 责编 | 徐威龙 封图| CSDN 下载于视觉中国 Spark3.0已经发布有一阵子了,官方发布了预览版,带来了一大波更新,对于我们程序员来说,首先当然是代码拉过来,打个包,跑 ...
- 我在windows10下,使用CMake gui 编译krita源码
系列文章目录 文章目录 系列文章目录 前言 一.krita编译说明 二.使用步骤 前言 我在windows10下,使用CMake gui 编译krita源码 where is the source c ...
- 我在windows10下,使用CMake gui 编译krita源码,CMake gui报错:LibMyPaint_DIR-NOTFOUND
系列文章目录 文章目录 系列文章目录 前言 一.原因 二.解决 1.引入库 前言 我在windows10下,使用CMake gui 编译krita源码 where is the source code ...
- yolov5检测小目标(附源码)
yolov5小目标检测(图像切割法附源码) 6.30 更新切割后的小图片的label数据处理 前言 yolov5大家都熟悉,通用性很强,但针对一些小目标检测的效果很差. YOLOv5算法在训练模型的过 ...
- OpenCvSharp (C# OpenCV) DNN模块加载自己训练的TensorFlow模型做目标检测(含手势识别、骰子识别、菜品识别)(附源码)
本文作者Color Space,文章未经作者允许禁止转载! 本文将介绍OpenCVSharp DNN模块加载自己训练的TensorFlow模型做目标检测(含手势识别.骰子识别.菜品识别)! 前言: 下 ...
最新文章
- ffmpeg文件拼接
- 常考数据结构与算法:找到字符串的最长无重复字符子串
- 开机explorer无法启动,无法进入桌面
- 现代制造工程笔记05-表面工程技术
- 【AI视野·今日CV 计算机视觉论文速览 第190期】Fri, 9 Apr 2021
- 诗与远方:无题(九十二)
- koajs 项目实战(二)
- 模糊滤镜_如何用 PS,为照片增加模糊与动感效果
- java poi html转PDF_实现word转pdf,HTML转pdf(探索篇)
- KETTLE相关问题处理
- kafka 验证_KAFKA:简单的验证码实施
- 教你windows10系统更改任务栏颜色教程
- java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager acti
- 查看及更改计算机名或域,如何查看和修改计算机名、域和工作组
- 删除容器docker rm和强制删除容器docker rm -f
- 年度盘点丨产品在线帮助文档/中心怎么做?看完这4个案例就知道
- 交流计量芯片HLW8112的校准方法
- Django 搭建博客网站-task01:基础知识
- bat文件打开后闪退如何解决
- 简师网:教师编只能在所考地有用吗?