当前位置:网站首页>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。
边栏推荐
- Digital transformation: data visualization enables sales management
- Generate random numbers (4-bit, 6-bit)
- tensorflow2-savedmodel convert to tflite
- 生成随机数(4位、6位)
- [zero basic IOT pwn] reproduce Netgear wnap320 rce
- Pat 1065 a+b and C (64bit) (20 points) (16 points)
- Cannot link redis when redis is enabled
- sqlilabs less10
- 数字化转型:数据可视化赋能销售管理
- TypeScript:const
猜你喜欢

Details of appium key knowledge

In hot summer, please put away this safe gas use guide!
![[14. Interval sum (discretization)]](/img/e5/8b29aca7068a6385e8ce90c2742c37.png)
[14. Interval sum (discretization)]

How to view the state-owned enterprises have unloaded Microsoft office and switched to Kingsoft WPS?

JVM performance tuning and practical basic theory part II

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

Don't want to knock the code? Here comes the chance

Word2vec yyds dry goods inventory
![[15. Interval consolidation]](/img/6c/afc46a0e0d14127d2c234ed9a9d03b.png)
[15. Interval consolidation]

Problem note - Oracle 11g uninstall
随机推荐
2022-2-15 learning xiangniuke project - Section 4 business management
Buuctf reinforcement question ezsql
JVM performance tuning and practical basic theory part II
Problem note - Oracle 11g uninstall
solidty-基础篇-结构体和数组,私有 / 公共函数,函数的返回值和修饰符,事件
Mongodb second talk - - mongodb High available Cluster Implementation
购物商城6.27待完成
[leetcode 324] 摆动排序 II 思维+排序
Use the npoi package of net core 6 C to read excel Pictures in xlsx cells and stored to the specified server
Research Report on the development trend and competitive strategy of the global powder filling machine industry
Tensorflow 2. X realizes iris classification
Day-02 database
手把手带你入门 API 开发
En utilisant le paquet npoi de net Core 6 c #, lisez Excel.. Image dans la cellule xlsx et stockée sur le serveur spécifié
[Verilog quick start of Niuke question series] ~ use functions to realize data size conversion
Advanced C language
[stage life summary] I gave up the postgraduate entrance examination and participated in the work. I have successfully graduated and just received my graduation certificate yesterday
Fundamentals of C language
Yyds dry goods inventory hcie security day13: firewall dual machine hot standby experiment (I) firewall direct deployment, uplink and downlink connection switches
sqlilabs less10