当前位置:网站首页>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。
边栏推荐
- Fundamentals of C language
- [getting started with Django] 13 page Association MySQL "multi" field table (check)
- Research Report on development trend and competitive strategy of global vibration polishing machine industry
- 2022-2-15 learning the imitation Niuke project - Section 3 post details
- Build MySQL master-slave server under Ubuntu 14.04
- Written on the first day after Doris graduated
- 建立自己的网站(14)
- tensorflow2-savedmodel convert to pb(frozen_graph)
- Research Report on the development trend and competitive strategy of the global navigation simulator industry
- Develop small programs and official account from zero [phase III]
猜你喜欢

【15. 区间合并】

博文推荐 | 深入研究 Pulsar 中的消息分块

MIT team used graph neural network to accelerate the screening of amorphous polymer electrolytes and promote the development of next-generation lithium battery technology

Microservice development steps (Nacos)

Guess lantern riddles, not programmers still can't understand?

【14. 区间和(离散化)】

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

C learning notes (5) class and inheritance
![[15. Interval consolidation]](/img/6c/afc46a0e0d14127d2c234ed9a9d03b.png)
[15. Interval consolidation]

音乐播放器开发实例(可毕设)
随机推荐
30 Devops interview questions and answers
Research Report on the development trend and competitive strategy of the global traditional computer industry
Research Report on the development trend and competitive strategy of the global axis measurement system industry
Research Report on development trend and competitive strategy of global vibration polishing machine industry
对于编程思想和能力有重大提升的书有哪些?
Research Report on the development trend and competitive strategy of the global ultrasonic scalpel system industry
建立自己的网站(14)
竣达技术丨室内空气环境监测终端 pm2.5、温湿度TVOC等多参数监测
SQLAchemy 常用操作
Research Report on the development trend and competitive strategy of the global powder filling machine industry
Zabbix API与PHP的配置
JVM performance tuning and practical basic theory part II
Develop small programs and official account from zero [phase III]
About the use of HTTP cache validation last modified and Etag
Opencv mat class
The markdown editor uses basic syntax
Research Report on the development trend and competitive strategy of the global commercial glassware industry
2022-2-15 learning the imitation Niuke project - Section 3 post details
Digital transformation: data visualization enables sales management
职场太老实,总被欺负怎么办?