侵权投诉
订阅
纠错
加入自媒体

Cmake学习总结(三)

2020-07-13 09:13
TXP嵌入式
关注

大家好,在上一篇文Cmake文章里面,我们同样在文章的最后面留了一个问题实现,就是把源文件放到src目录下,把头文件放到include目录下去,这样也比较符合别人和自己日后去配置工程(一看到这两个目就能知道啥意思了,清晰明了),同时在linux环境下生成的elf文件放到bin目录下;不过在文章发出去了几天,后面有网友又有提出了一些新的需求:

Cmake学习总结(三)

(如果网友有啥实际需要,可以私聊我,只要在我自身能力之内,我都可以写成文章出来分享给大家)熟悉我的网友都知道,我也是小白,会从很基础的东西开始分享开始,虽然都是比较理论化的东西,但是都是点滴的积累(有的时候,其实你真正在有些项目开发过程中,学到的东西不是很多,更多的是依靠平时的基础积累加以扩展,所以总的来说,平时的折腾还是非常值得!);同时有啥比较实际一点的需求咋也慢慢深入,一步步来,稳扎稳打,知识性的东西来不得半点虚假和马虎。好了,开始进入主题分享了:

一、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

1  2  3  4  下一页>  
声明: 本文由入驻维科号的作者撰写,观点仅代表作者本人,不代表OFweek立场。如有侵权或其他问题,请联系举报。

发表评论

0条评论,0人参与

请输入评论内容...

请输入评论/评论长度6~500个字

您提交的评论过于频繁,请输入验证码继续

暂无评论

暂无评论

电子工程 猎头职位 更多
扫码关注公众号
OFweek电子工程网
获取更多精彩内容
文章纠错
x
*文字标题:
*纠错内容:
联系邮箱:
*验 证 码:

粤公网安备 44030502002758号