cmake入門實戰基礎

2021-09-12 08:35:29 字數 2161 閱讀 4306

專案例項目錄結構:主要實現了包含了專案內部生成的庫,同時又依賴了外部三方的庫。

├── bin

│   └── demo_cmake

├── build

├── cmakelists.txt

├── inc

│   └── mathcommon.h

├── lib

│   ├── libmathcommon.so

│   └── libmathfunctions.a

├── lib_extern

│   ├── build

│   ├── cmakelists.txt

│   ├── mathcommon.cpp

│   └── mathcommon.h

├── lib_inner

│   ├── cmakelists.txt

│   ├── mathfunctions.cpp

│   └── mathfunctions.h

├── main.cpp

├── untils.cpp

└── untils.h

主目錄下的cmakelists.txt內容:

#cmake最低版本要求

cmake_minimum_required(version 2.8)

#專案名稱

project(demo_cmake)

#專案預設的執行檔案目錄和源**目錄

message(status $)

message(status $)

message(status $)

message(status $)

#set(cmake_build_type "debug")

#set(cmake_cxx_flags_debug "$env -o0 -wall -g -ggdb")

#set(cmake_cxx_flags_release "$env -o3 -wall")

#設定庫輸出目錄

set(library_output_path $/lib)

#設定可執行檔案輸出目錄

set(executable_output_path $/bin)

#頭檔案目錄

#指定標頭檔案的搜尋路徑,相當於指定gcc的-i引數

include_directories($/inc)

#鏈結庫目錄

#動態鏈結庫或靜態鏈結庫的搜尋路徑,相當於gcc的-l引數

link_directories($/lib)

#查詢目錄下所有的原始檔並儲存在變數src_dir中

aux_source_directory(. src_dir)

message(status $)

#包含子目錄

add_subdirectory(lib_inner)

#編譯可執行程式

#add_executable(demo main.cpp common.cpp)

add_executable(demo_cmake $)

#新增鏈結庫,相同於指定-l引數

target_link_libraries(demo_cmake mathfunctions mathcommon)

專案內部生成庫cmakelists.txt:

aux_source_directory(. lib_src_dir)

add_library(mathfunctions static $)

#add_library(mathfunctions shared $)

專案外部生成庫cmakelists.txt:

cmake_minimum_required(version 2.8)

aux_source_directory(. so_extern_dir)

add_library(mathcommon shared $)

主目錄main函式**:

1 #include2 

3 #include"untils.h"

4 5 #include"lib_inner/mathfunctions.h"

6 7 #include"mathcommon.h"

8 9 int main()

10

CMake入門實戰

編寫cmake配置檔案cmakelists.txt。執行命令cmake path或者ccmake path生成makefile。其中,path是cmakelists.txt所在的目錄。使用make命令進行編譯。首先編寫 cmakelists.txt 檔案,並儲存在與main.cc原始檔同個目錄下 c...

CMake簡易入門

首發於fxm5547的部落格 cmake minimum required version 2.6 project itest c 標準 set cmake cxx standard 11 指定參與編譯的原始檔 add executable itest src main.cpp src cal ca...

cmake 學習入門

1.cmake 的學習入口 cmake 手冊文件一把一把的,就是不帶tutorial,ubuntu16 下的apt檔案就是這樣,無語了 我不需要讀那麼多,我只需要乙個tutorial就可以了,而tutorial只能從網上不能從man手冊或 apt檔案中得到了.不過github上有很多例子,也有tut...