CMake 簡單教程

2021-08-01 23:11:38 字數 3545 閱讀 3555

sudo add-apt-repository ppa:george-edison55/cmake-3.x

sudo apt-get update

sudo apt-get install cmake

更多變數查詢位址

# project的名稱

project (caffe_framework)

# 最低版本限制

cmake_minimum_required (version 3.0)

# 獲取`cmakelist.txt`所在目錄

set(projdir $)

常用

+set(a "hello")

該命令不會重寫cache中的值

+set(a "hello" cache)

如果cache存在該變數,使用cache中變數

如果cache中不存在,將該值寫入cache

note: 彷彿cmake_開頭的變數都會自帶乙個空的」「cache, 例如如果通過這條命令給cmake_build_type賦值,可能不會改變它的值,它仍然會用最開始的空cache.

+set(a "hello" cache force)

不論cache中是否存在,始終使用該值

# 顯示所有編譯命令

set(cmake_verbose_makefile on)

# 安裝路徑字首,之後的`install()`命令都安裝在該路徑下

set(cmake_install_prefix "~/build" cache path "" force)

# 在執行cmake的目錄生成乙個lib資料夾,之後生成的lib會放在此資料夾下

set(library_output_path ./lib)

if(cmake_build_type strequal

"") set(cmake_build_type release cache string

"default to release" force)

endif()

# 選擇乙個目錄下的所有源**

aux_source_directory(./src/ src_files_1)

# 選擇另乙個目錄下的所有源**

aux_source_directory(./src/math src_files_2)

# 為visual studio的原始碼設定檔案結構(不設定的話所有原始碼都在乙個目錄)

source_group(src files $)

source_group(src\\math files $)

# 指定標頭檔案路徑,類似 -i

include_directories(./include ./)

# 指定庫檔案路徑,類似 -l

link_directories(./lib $/lib)

# 新增靜態庫

add_library(libname $

$)# 為某目標新增依賴庫

target_link_libraries(libname lib1 -wl,--whole-archive -lcnn -wl,--no-whole-archive lib2)

# 設定安裝路徑

set(cmake_install_prefix /usr/local)

# 安裝至cmake_install_prefix下的目錄

install(targets libname archive destination lib )

install(directory include/ destination include files_matching pattern "*.h" pattern "*.hpp")

# 分支判斷。字串判斷必須用strequal。equal用於判斷number

if(str strequal

"hello")

endif()

# 新增子目錄,exclude_from_all表示不會自動編譯子目錄

add_subdirectory(./demo exclude_from_all)

# 新增依賴

add_dependencies(target-name depend-target1 depend-target2 .. )

file(glob_recurse proj_cpp_sources

$/include/*.h

$/include/*.hpp

$/src/*.h

$/src/*.hpp

$/src/*.cpp

$/src/*.cc

)source_group_by_dir( proj_cpp_sources projdir )

if(del_one)

list( remove_item proj_cpp_sources $/src/lucky_one.cpp)

endif()

#debug release 使用不同配置

target_link_libraries( facedetction

$<$:opencv_core2411d>

$<$:opencv_core2411>

$<$:opencv_imgproc2411d>

$<$:opencv_imgproc2411>

$<$:opencv_highgui2411d>

$<$:opencv_highgui2411>

otherlibs

)

file(read "./.git/logs/head" git_logs)

string(regex replace ";"

"\\\\;" git_logs "$")

string(regex replace "'"

"-" git_logs "$")

string(regex replace "\n"

";" git_logs "$")

# get the last line

list(reverse git_logs )

list(get git_logs 1 git_logs)

set_target_properties(target properties debug_postfix _d)
set(__core_names "fastcnn"

"nvcaffe"

"baseforwardcnn")

set(__core_name_default "fastcnn")

set(topni_core_name $ cache string

"select core architecture.")

set_property( cache topni_core_name property strings ""

$ )

CMake的簡單應用教程

以編寫helloworld為例 這是我的資料夾 名1 下的檔案,build資料夾是後面使用mkdir命令建立的。其中main.cpp include using namespace std int main 生成可執行程式 語法 add executable 可執行程式名 要編譯的cpp add e...

CMake使用教程

編寫的測試的檔案目錄如下 cmaketest makelists.txt config.h.in main.cpp math makelists.txt mathfunction.cpp mathfunction.h下面為每個檔案中的內容,函式的功能是分別利用自定義的庫和標準庫實現求乙個數的冪次方 ...

工具 cmake教程

檔名稱 cmakelists.txt 檔案內容 乙個總工程的根目錄 宣告要求的 cmake 最低版本 cmake minimum required version 2.8 宣告乙個 cmake 工程 project robotics 設定編譯模式 set cmake build type debug...