乙個簡單的CMake工程示例

2021-08-01 23:27:38 字數 2710 閱讀 5401

在工程目錄下,構建目錄src,include,lib,bin。在src目錄下存放源**檔案,include目錄下存放標頭檔案,lib目錄用於存放生成的庫(動態庫或者靜態庫),bin目錄存放最終生成的可執行檔案。

src目錄存放main.cpp和lib_demo.cpp的原始檔:

main.cpp

#include #include "main.h"

#include "lib_demo.h"

using namespace std;

int main()

void print_project_msg()

lib_demo.cpp

#include #include "lib_demo.h"

using namespace std;

void lib_demo()

include 目錄下的標頭檔案為:

main.h

#ifndef __main_h__

#define __main_h__

void print_project_msg();

#endif /*__main_h__*/

lib_demo.h

#ifndef __lib_demo__

#define __lib_demo__

void lib_demo();

#endif /*__lib_demo__*/

在工程的根目錄和src目錄下各有有乙個cmakelists.txt檔案:

工程根目錄cmakelists.txt

#cmake最低版本號要求

cmake_minimum_required(version 2.8)

#指定專案名稱

project(cmakedemo)

#指定版本資訊

set(cmake_system_version 1)

#若是需要指定編譯器路徑

#set(cross_toolchain_prefix "/path/arm-linux-")

#指定編譯器

#set(cmake_c_compiler   "$gcc")

#set(cmake_cxx_compiler "$g++")

#使用預設路徑的g++指定編譯器  

set(cmake_cxx_compiler "g++")

#指定編譯選項

set(cmake_build_type debug )

#指定編譯目錄

set(project_binary_dir $/build)

#新增子目錄,這樣進入原始碼檔案src目錄可以繼續構建  

add_subdirectory($/src)

src目錄下的cmakelists.txt:

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

#並將名稱儲存到dir_lib_srcs目錄

#aux_source_directory(. dir_lib_srcs)

#指定頭檔案目錄,project_source_dir為工程的根目錄  

include_directories($/include)

#指定可執行檔案的輸出目錄,輸出到bin下面  

set(executable_output_path $/bin)

#生成動態庫    

add_library(shared_demo shared lib_demo.cpp)

#設定庫輸出名為 shared => libshared.so  

set_target_properties(shared_demo properties output_name "shared")

#生成靜態庫  

add_library(static_demo static lib_demo.cpp)  

#設定輸庫出名為 static => libstatic.a  

set_target_properties(static_demo properties output_name "static")

#指定庫檔案輸出路徑  

set(library_output_path $/lib)

#在指定目錄下查詢庫,並儲存在libpath變數中

find_library(libpaths  shared $/lib)  

#指定生成目標

add_executable(main main.cpp)

#鏈結共享庫

target_link_libraries(main $)

整個工程的目錄檔案架構如下:

在專案根目錄新建目錄build,進入build,執行cmake ..命令,若沒有錯誤,在執行make命令。

在bin目錄下產生可執行檔案,lib中生成庫檔案。

另外:aux_source_directory(directory var)

將指定的directory目錄下的所有原始檔儲存到var變數中,

例如:aux_source_directory(src src_cpp)將src目錄下的原始檔儲存到src_cpp目錄中。

乙個游標簡單示例

下面是乙個游標的例項,方便初學者學習,也可以防止自己忘記 if exists select 1 from sysobjects where name hehe drop procedure hehe goset ansi nulls on set quoted identifier on gocre...

乙個簡單的學習示例

bob bob smith 42,30000,software sue sue jones 45,40000,hardware print bob 0 sue 2 print n print bob 0 split 1 sue 2 1.25 print n print sue people bob,...

乙個簡單的Quartz示例

1 在你使用排程器 scheduler 之前,需要先對其進行初始化。你可以使用乙個排程器工廠 schedule ctory 來完成這項任務。一些quartz的使用者可能會在jndi中儲存乙個工廠 factory 的例項,但是其他使用者可能會直接使用乙個工廠例項 就像下面的示例一樣 並發現初始化乙個排...