CMake使用教程

2021-09-22 18:05:43 字數 2992 閱讀 9435

編寫的測試的檔案目錄如下:

cmaketest

|+--- makelists.txt

|+--- config.h.in

|+--- main.cpp

|+--- math/

|+--- makelists.txt

|+--- mathfunction.cpp

|+--- mathfunction.h

下面為每個檔案中的內容,函式的功能是分別利用自定義的庫和標準庫實現求乙個數的冪次方

1、根目錄中的makelists.txt

# cmake 最低版本號要求

cmake_minimum_required (version 2.8)

# 專案資訊

project (demo5)

#加入乙個配置標頭檔案,用於處理cmake對原始碼的設定

configure_file (

"$/config.h.in"

"$/config.h"

)#是否使用自己的mathfunction庫

option (use_mymath "use provided math implementation" on)

#是否加入mathfunction庫

if (use_mymath)

include_directories ("$/math")

add_subdirectory (math)

set (extra_libs $ mathfunction)

endif (use_mymath)

#查詢當前目錄下所有原始檔 並將名稱儲存到dir_srcs變數中

aux_source_directory(. dir_srcs)

#新增math子目錄

#add_subdirectory(math)

#制定生成目標

add_executable(demo5 $)

#新增查詢標頭檔案

#include_directories("$/math")

#新增連線庫

target_link_libraries(demo5 $)

# 指定安裝路徑

install (targets demo5 destination bin)

install (files "$/config.h" destination include)

# 啟用測試

enable_testing()

# 測試 5 的平方

add_test (test_5_2 demo5 5 2)

set_tests_properties (test_5_2

properties pass_regular_expression "is 100")

2、根目錄中的config.h.in,用來配置**中的配置選項

#cmakedefine use_mymath
3、根目錄中的main.cpp

#include #include #include "config.h"

#ifdef use_mymath

#include "mathfunction.h"

#else

#include #endif

int main(int argc, char *ar**)

double base = atof(ar**[1]);

int exponent = atoi(ar**[2]);

#ifdef use_mymath

double result = power(base, exponent);

printf(" use my math %g ^ %d is %g\n", base, exponent, result);

#else

double result = pow(base, exponent);

printf(" use standard math %g ^ %d is %g\n", base, exponent, result);

#endif

return 0;

}

4、math目錄中的makelists.txt

# 查詢當前目錄下的所有原始檔

# 並將名稱儲存到 dir_lib_srcs 變數

aux_source_directory(. dir_lib_srcs)

# 生成鏈結庫

add_library (mathfunction $)

#指定mathfunctions庫的安裝路徑

install (targets mathfunction destination bin)

install (files mathfunction.h destination include)

5、math目錄中的mathfunction.cpp

/**

* power - calculate the power of number.

* @param base: base value.

* @param exponent: exponent value.

* * @return base raised to the power exponent.

*/#include "mathfunction.h"

double power(double base, int exponent)

for(i = 1; i < exponent; ++i)

return result;

}

6、math目錄中的mathfunction.h

#ifndef _mathfunction_h_

#define _mathfunction_h_

double power(double base, int exponent);

#endif

cmake使用教程

cmake minimum required version 3.0 project main include directories include 設定為cmmakelists.txt所在的路徑為標頭檔案搜尋路徑 link dirctories lib add executable main m...

CMake 簡單教程

sudo add apt repository ppa george edison55 cmake 3.x sudo apt get update sudo apt get install cmake更多變數查詢位址 project的名稱 project caffe framework 最低版本限制...

工具 cmake教程

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