当前位置:网站首页>cmake 基本使用过程
cmake 基本使用过程
2022-07-01 14:51:00 【JACKSONMHLN】
1、完整项目的目录
一个完整的项目,应当至少分为这几个部分。其中,bin目录下是可运行文件,build目录下是临时构建的文件,include目录下是用到的头文件,src目录下是头文件的实现文件。当然还可以加上test测试文件,third第三方库文件等

2、cmake的使用
cmake使用简单方便,可以跨平台,构建项目编译环境。尤其比直接写Makefile简单(在构建大型工程编译 时,需要写大量的文件依赖关系),可以通过简单的CMake生成负责的Makefile文件。我们经常使用是外部构建,
使用外部构建,我们可以创建一个可以位于文件系统上任何位置的构建文件夹。 所有临时构建和目标文件都位于此目录中,以保持源代码树的整洁。(可以指定目标文件的位置)。一上面的例子为例:
cd /build/ 转到build目录下
cmake .. cmake 因为工程的CMakeLists.txt 在上个级别,因此有 ..
make 上一步会生成makefile文件,make执行。
3、CMakeLists.txt 的编写
cmake_minimum_required(VERSION 3.10)
#指定camke的最小版本, 可以指定版本的最低到最高
project(sum1)
#设置 工程名
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#指定 生成的可执行文件的 存放目录
set(SOURCES
src/sum.cpp)
main.cpp)
#创建一个变量,名字叫SOURCES。它包含了指定的cpp文件
add_executable(sum11 ${SOURCES})
#用所有的源文件生成一个可执行文件,因为这里定义了SOURCE变量,所以就不需要罗列cpp文件了
target_include_directories(sum11
PRIVATE
${PROJECT_SOURCE_DIR}/include)
#设置这个可执行文件sum11需要包含的库的路径,注意是可执行文件 sum11 需要包含的库的路径。编写完这个 CMakeList.txt 文件后,就可以按照 步骤2 所说的去操作。
上文所描述的是简单的一个步骤,随着工程的复杂,需要编写的变多。对其中一些常见的量进行一个总结与区分。
1、aux_source_directory(<dir> <variable>) 与 set(<variable> target.cpp)
在目录中查找所有源文件。收集指定目录中所有源文件的名称,并将列表存储在提供的变量中。
set(SOURCES src/sum.cpp) 收集指定目录下的指定源文件,并将存储。
2、include_directories(${PROJECT_SOURCE_DIR}/include) 与
target_include_directories(sum11 PRIVATE ${PROJECT_SOURCE_DIR}/include)
作用:给源文件添加头文件搜索路径:将指定目录添加到编译器的头文件搜索路径之下,指定的目录被解释成当前源码路径的相对路径。
第一个:当前CMakeList.txt中的所有目标以及所有在其调用点之后添加的子目录中的所有目标将具有头文件搜索路径。
第二个:指定目标包含的头文件路径。
中间属性的用法:<INTERFACE|PUBLIC|PRIVATE>使用依据
- PRIVATE - 目录被添加到目标(库)的包含路径中。
- INTERFACE - 目录没有被添加到目标(库)的包含路径中,而是链接了这个库的其他目标(库或者可执行程序)包含路径中
- PUBLIC - 目录既被添加到目标(库)的包含路径中,同时添加到了链接了这个库的其他目标(库或者可执行程序)的包含路径中
也就是说,根据库是否包含这个路径,以及调用了这个库的其他目标是否包含这个路径,可以分为三种scope。
边栏推荐
- Minimum spanning tree and bipartite graph in graph theory (acwing template)
- Vnctf2022 open web gocalc0
- Research Report on the development trend and competitive strategy of the global high temperature label industry
- 对于编程思想和能力有重大提升的书有哪些?
- Basic operations of SQL database
- 2022-2-15 learning xiangniuke project - Section 1 filtering sensitive words
- What problems should be considered for outdoor LED display?
- Markdown编辑器使用基本语法
- Reorganize the trivial knowledge points at the end of the term
- Research Report on the development trend and competitive strategy of the global diamond suspension industry
猜你喜欢

【牛客网刷题系列 之 Verilog快速入门】~ 多功能数据处理器、求两个数的差值、使用generate…for语句简化代码、使用子模块实现三输入数的大小比较

Official announcement: Apache Doris graduated successfully and became the top project of ASF!

关于重载运算符的再整理

首届技术播客月开播在即

问题随记 —— Oracle 11g 卸载

What problems should be considered for outdoor LED display?

MIT团队使用图神经网络,加速无定形聚合物电解质筛选,促进下一代锂电池技术开发

2022-2-15 learning xiangniuke project - Section 1 filtering sensitive words

2022-2-15 learning the imitation Niuke project - Section 3 post details

Internet hospital system source code hospital applet source code smart hospital source code online consultation system source code
随机推荐
[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize
2022-2-15 learning the imitation Niuke project - Section 3 post details
APK签名原理
三十之前一定要明白的职场潜规则
ArrayList 扩容详解,扩容原理[通俗易懂]
Problem note - Oracle 11g uninstall
音乐播放器开发实例(可毕设)
2022-2-15 learning xiangniuke project - Section 4 business management
Cannot link redis when redis is enabled
Solid smart contract development - easy to get started
Markdown编辑器使用基本语法
Vnctf2022 open web gocalc0
111. Minimum depth of binary tree
深度分析数据在内存中的存储形式
SQLAchemy 常用操作
Research Report on the development trend and competitive strategy of the global aviation leasing service industry
Pat 1121 damn single (25 points) set
首届技术播客月开播在即
tensorflow2-savedmodel convert to pb(frozen_graph)
JVM第二话 -- JVM内存模型以及垃圾回收