大家好,在上一篇文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文章
795瀏覽量
41691 -
嵌入式設計
+關注
關注
0文章
392瀏覽量
21304
發布評論請先 登錄
相關推薦
評論