大家好,在上一篇文Cmake文章里面,我们同样在文章的最后面留了一个问题实现,就是把源文件放到src目录下,把头文件放到include目录下去,这样也比较符合别人和自己日后去配置工程(一看到这两个目就能知道啥意思了,清晰明了),同时在linux环境下生成的elf文件放到bin目录下;不过在文章发出去了几天,后面有网友又有提出了一些新的需求:
(如果网友有啥实际需要,可以私聊我,只要在我自身能力之内,我都可以写成文章出来分享给大家)熟悉我的网友都知道,我也是小白,会从很基础的东西开始分享开始,虽然都是比较理论化的东西,但是都是点滴的积累(有的时候,其实你真正在有些项目开发过程中,学到的东西不是很多,更多的是依靠平时的基础积累加以扩展,所以总的来说,平时的折腾还是非常值得!);同时有啥比较实际一点的需求咋也慢慢深入,一步步来,稳扎稳打,知识性的东西来不得半点虚假和马虎。好了,开始进入主题分享了:
一、src、include、bin目录的使用(更加正规化):
1、先开始创建这三个目录结构,并把相应的文件放入进去:
root@txp-virtual-machine:/home/txp/testmy# mkdir bin build src include
root@txp-virtual-machine:/home/txp/testmy# ls
bin build include src
include目录下文件放入(这里test1.h和test2.h的内容是接续前面的文章里面的内容,这里我就不再造轮子了):
root@txp-virtual-machine:/home/txp/testmy/include# ls
test1.h test2.h
src目录下文件放入(这里test1.c和test2.c的内容是接续前面的文章里面的内容,这里我就不再造轮子了):
root@txp-virtual-machine:/home/txp/testmy/src# ls
main.c test1.c test2.c
最终我们还要在testmy目录和src目录下都创建一个CMakeLists.txt:
/*testmy目录下的CMakeLists.txt内容:*/
cmake_minimum_required(VERSION 2.8)
project(main)
/*src目下CMakeLists.txt内容:*/
aux_source_directory(. SRC_LIST)
include_directories(../include)
add_executable(main ${SRC_LIST})
set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
上面第一个CMakeLists.txt里面陌生的语句解释:
add_subdirectory(src)意思是可以向当前工程添加存放源文件的子目录,并可以指定中间二进制和目标二进制的存放位置(subdirectory字母就是子目录的意思,所以意思是:这里指定src目录下存放了源文件,当执行cmake时,就会进入src目录下去找src目录下的CMakeLists.txt,所以在src目录下也建立一个CMakeLists.txt),官方用法是这样的(不过这里暂时没去深究):
add_subdirectory
----------------
Add a subdirectory to the build.
::
add_subdirectory(source_dir [binary_dir]
[EXCLUDE_FROM_ALL])
Add a subdirectory to the build. The source_dir specifies the
directory in which the source CMakeLists.txt and code files are
located. If it is a relative path it will be evaluated with respect
to the current directory (the typical usage), but it may also be an
absolute path. The binary_dir specifies the directory in which to
place the output files. If it is a relative path it will be evaluated
with respect to the current output directory, but it may also be an
absolute path. If binary_dir is not specified, the value of
source_dir, before expanding any relative path, will be used (the
typical usage). The CMakeLists.txt file in the specified source
directory will be processed immediately by CMake before processing in
the current input file continues beyond this command.
If the EXCLUDE_FROM_ALL argument is provided then targets in the
subdirectory will not be included in the ALL target of the parent
directory by default, and will be excluded from IDE project files.
Users must explicitly build targets in the subdirectory. This is
meant for use when the subdirectory contains a separate part of the
project that is useful but not necessary, such as a set of examples.
Typically the subdirectory should contain its own project() command
invocation so that a full build system will be generated in the
subdirectory (such as a VS IDE solution file). Note that inter-target
dependencies supercede this exclusion. If a target built by the
parent project depends on a target in the subdirectory, the dependee
target will be included in the parent project build system to satisfy
the dependency.
第二个CMakeLists.txt内容分析:
aux_source_directory (. SRC_LIST):把当前目录的源文件:main.c test1.c test2.c都放到变量SRC_LIST里面去。
include_directories (../include):把include目录的头文件包含进来。
set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin):这里面的EXECUTABLE_OUT_PATH和PROJECT_SOURCE_DIR是CMake自带的预定义变量,同时他们的作用分别如下:
EXECUTABLE_OUTPUT_PATH :目标二进制可执行文件的存放位置
PROJECT_SOURCE_DIR:工程的根目录
所以最终生成的elf文件(也就是我们的最终可执行文件)就会放到bin目录下,然后我们build目录下会成一些配置中间文件。
具体步骤过程我写出来:
root@txp-virtual-machine:/home/txp/testmy# vim CMakeLists.txt
root@txp-virtual-machine:/home/txp/testmy# cd src
root@txp-virtual-machine:/home/txp/testmy/src# ls
main.c test1.c test2.c
root@txp-virtual-machine:/home/txp/testmy/src# vim CMakeLists.txt
-
二进制
+关注
关注
2文章
760浏览量
41448 -
嵌入式设计
+关注
关注
0文章
390浏览量
21209
发布评论请先 登录
相关推荐
执行build.py menuconfig时报“ Unknown CMake command \"__add_uf2_targets\".”错误,请问是什么原因?
idf.py menuconfig的时候总是提示cmake301 error的原因?
刚刚装好esp32 elipse CMAKE编译环境,hello word编译不通过怎么解决?
VSCode中编译过程cmake错误的原因?
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-如何移植NCNN?
请问一下CMake和Make之间的区别有哪些?
无人机测深的三种方法总结
![无人机测深的<b class='flag-5'>三</b>种方法<b class='flag-5'>总结</b>](https://file1.elecfans.com/web2/M00/BB/F9/wKgaomWXeQSAfh6AAAFOF4YmdO8191.jpg)
Windows下编译工具CMake的安装和最简使用
![Windows下编译工具<b class='flag-5'>CMake</b>的安装和最简使用](https://file1.elecfans.com/web2/M00/AF/E1/wKgZomVS2U2ASacQAAAPVCut6ZY500.png)
CMake构建后的项目结构解析
![<b class='flag-5'>CMake</b>构建后的项目结构解析](https://file1.elecfans.com/web2/M00/AD/68/wKgaomVNlNqABZE0AACQ_0qe0Es164.jpg)
在Linux下如何使用CMake编译程序
![在Linux下如何使用<b class='flag-5'>CMake</b>编译程序](https://file1.elecfans.com/web2/M00/AD/0C/wKgaomVLO4mARvmyAABniPPNX20357.jpg)
全面总结机器学习中的优化算法
![全面<b class='flag-5'>总结</b>机器<b class='flag-5'>学习</b>中的优化算法](https://file1.elecfans.com/web2/M00/AD/CB/wKgZomVDB-KASSjzAAALlMQaI04382.png)
评论