大家好,在上一篇文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)
add_subdirectory(src)
/*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
什么是 D类音频功率放大器
5G | 禁令升级!特朗普针对华为签“紧急状态”令
实验室生物毒性仪的功能特点都有哪些
电池行业洗牌惨烈,国能电池倒闭?官方出面辟谣
美国微芯科技公司推出两款全新的tinyAVR® MCU器件
Cmake学习的总结(三)
变频器制动电阻的作用原理
“中国芯”厂商盘点(上):紫光展锐/中芯之外还有谁?
工信部:展望2021年5G发展工作重点
iphone8将巨变 8大靠谱曝光盘点
佳能1.2亿像素图像传感器120MXSI和全画幅图像传感器35MMFHDXSMA介绍
激光技术的应用
摩托罗拉新型智能手机G30在海外上市
三星发布第二款采用8nm工艺打造的SoC芯片
多层沉金线路板的优缺点分析
小象智能车载XR智能座舱技术优势
韩国已制定出了半自动驾驶汽车的安全标准
诺基亚失去芬兰最有价值公司地位
三星折叠屏手机Galaxy Fold将在4月16日进行真机开箱
沃达丰将于2019年中在英国全面推出5G服务