大家好,在上一篇文Cmake文章里面,我們同樣在文章的最后面留了一個(gè)問(wèn)題實(shí)現(xiàn),就是把源文件放到src目錄下,把頭文件放到include目錄下去,這樣也比較符合別人和自己日后去配置工程(一看到這兩個(gè)目就能知道啥意思了,清晰明了),同時(shí)在linux環(huán)境下生成的elf文件放到bin目錄下;不過(guò)在文章發(fā)出去了幾天,后面有網(wǎng)友又有提出了一些新的需求:
(如果網(wǎng)友有啥實(shí)際需要,可以私聊我,只要在我自身能力之內(nèi),我都可以寫(xiě)成文章出來(lái)分享給大家)熟悉我的網(wǎng)友都知道,我也是小白,會(huì)從很基礎(chǔ)的東西開(kāi)始分享開(kāi)始,雖然都是比較理論化的東西,但是都是點(diǎn)滴的積累(有的時(shí)候,其實(shí)你真正在有些項(xiàng)目開(kāi)發(fā)過(guò)程中,學(xué)到的東西不是很多,更多的是依靠平時(shí)的基礎(chǔ)積累加以擴(kuò)展,所以總的來(lái)說(shuō),平時(shí)的折騰還是非常值得!);同時(shí)有啥比較實(shí)際一點(diǎn)的需求咋也慢慢深入,一步步來(lái),穩(wěn)扎穩(wěn)打,知識(shí)性的東西來(lái)不得半點(diǎn)虛假和馬虎。好了,開(kāi)始進(jìn)入主題分享了:
一、src、include、bin目錄的使用(更加正規(guī)化):
1、先開(kāi)始創(chuàng)建這三個(gè)目錄結(jié)構(gòu),并把相應(yīng)的文件放入進(jìn)去:
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的內(nèi)容是接續(xù)前面的文章里面的內(nèi)容,這里我就不再造輪子了):
root@txp-virtual-machine:/home/txp/testmy/include# ls
test1.h test2.h
src目錄下文件放入(這里test1.c和test2.c的內(nèi)容是接續(xù)前面的文章里面的內(nèi)容,這里我就不再造輪子了):
root@txp-virtual-machine:/home/txp/testmy/src# ls
main.c test1.c test2.c
最終我們還要在testmy目錄和src目錄下都創(chuàng)建一個(gè)CMakeLists.txt:
/*testmy目錄下的CMakeLists.txt內(nèi)容:*/
cmake_minimum_required(VERSION 2.8)
project(main)
/*src目下CMakeLists.txt內(nèi)容:*/
aux_source_directory(. SRC_LIST)
include_directories(../include)
add_executable(main ${SRC_LIST})
set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
上面第一個(gè)CMakeLists.txt里面陌生的語(yǔ)句解釋?zhuān)?/p>
add_subdirectory(src)意思是可以向當(dāng)前工程添加存放源文件的子目錄,并可以指定中間二進(jìn)制和目標(biāo)二進(jìn)制的存放位置(subdirectory字母就是子目錄的意思,所以意思是:這里指定src目錄下存放了源文件,當(dāng)執(zhí)行cmake時(shí),就會(huì)進(jìn)入src目錄下去找src目錄下的CMakeLists.txt,所以在src目錄下也建立一個(gè)CMakeLists.txt),官方用法是這樣的(不過(guò)這里暫時(shí)沒(méi)去深究):
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.
第二個(gè)CMakeLists.txt內(nèi)容分析:
aux_source_directory (. SRC_LIST):把當(dāng)前目錄的源文件:main.c test1.c test2.c都放到變量SRC_LIST里面去。
include_directories (../include):把include目錄的頭文件包含進(jìn)來(lái)。
set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin):這里面的EXECUTABLE_OUT_PATH和PROJECT_SOURCE_DIR是CMake自帶的預(yù)定義變量,同時(shí)他們的作用分別如下:
EXECUTABLE_OUTPUT_PATH :目標(biāo)二進(jìn)制可執(zhí)行文件的存放位置
PROJECT_SOURCE_DIR:工程的根目錄
所以最終生成的elf文件(也就是我們的最終可執(zhí)行文件)就會(huì)放到bin目錄下,然后我們build目錄下會(huì)成一些配置中間文件。
具體步驟過(guò)程我寫(xiě)出來(lái):
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
-
二進(jìn)制
+關(guān)注
關(guān)注
2文章
804瀏覽量
42178 -
嵌入式設(shè)計(jì)
+關(guān)注
關(guān)注
0文章
393瀏覽量
21682
發(fā)布評(píng)論請(qǐng)先 登錄
雙極型三極管放大電路的三種基本組態(tài)的學(xué)習(xí)課件免費(fèi)下載
使用STM32CubeMX生成CMake工程中的FLASH.ld被更改怎么解決?
在CubeMX V6.13.0版本上配置的CMake工程無(wú)法通過(guò)編譯怎么解決?
為什么無(wú)法在OpenVINO? 2021.3源中使用CMAKE編譯ONNX模型?
TOF學(xué)習(xí)總結(jié)
關(guān)于中斷知識(shí)學(xué)習(xí)總結(jié)筆記
在學(xué)習(xí)go語(yǔ)言的過(guò)程踩過(guò)的坑
51單片機(jī)晶振的問(wèn)題總結(jié)
國(guó)產(chǎn)芯上運(yùn)行TinyMaxi輕量級(jí)的神經(jīng)網(wǎng)絡(luò)推理庫(kù)-米爾基于芯馳D9國(guó)產(chǎn)商顯板
人工智能、機(jī)器學(xué)習(xí)和深度學(xué)習(xí)是什么
執(zhí)行build.py menuconfig時(shí)報(bào)“ Unknown CMake command \"__add_uf2_targets\".”錯(cuò)誤,請(qǐng)問(wèn)是什么原因?
大模型技術(shù)及趨勢(shì)總結(jié)

評(píng)論