新聞中心
CMake 是一個(gè)跨平臺(tái)的自動(dòng)化構(gòu)建系統(tǒng),它使用一個(gè)名為 CMakeLists.txt 的文件來描述構(gòu)建過程,可以產(chǎn)生標(biāo)準(zhǔn)的構(gòu)建文件,如 Unix 的 Makefile 或Windows Visual C++ 的 projects/workspaces 。

成都創(chuàng)新互聯(lián)公司長期為成百上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為鹽邊企業(yè)提供專業(yè)的網(wǎng)站制作、成都網(wǎng)站制作,鹽邊網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
例1:Hello World
源代碼只有一個(gè)文件HelloWorld.cpp
#include
int main(int argc, char *argv[]){
std::cout "Hello World!" return 0;
}
123456
CMakeLists.txt也只有三行而已(使用cmake管理項(xiàng)目的過程,也就是編寫CMakeLists.txt的過程)
cmake_minimum_required(VERSION 2.8.9)
project (hello)
add_executable(hello helloworld.cpp)
123
第一行用于指定cmake最低版本 第二行指定項(xiàng)目名稱(這個(gè)名稱是任意的) 第三行指定編譯一個(gè)可執(zhí)行文件,hello是第一個(gè)參數(shù),表示生成可執(zhí)行文件的文件名(這個(gè)文件名也是任意的),第二個(gè)參數(shù)helloworld.cpp則用于指定源文件。
如果您電腦上已經(jīng)安裝了cmake,那么我們就已經(jīng)完事具備了。 第一步,用cmake生成Makefile文件
-
注:cmake命令后邊跟的就是CMakelist.txt所在的目錄,這個(gè)目錄不必是當(dāng)前目錄,你也可以新建一個(gè)build目錄或者其他名字的目錄來生成build文件,實(shí)際項(xiàng)目中也都是這么做的,這樣代碼會(huì)很干凈也便于git管理.
第二步,make編譯程序 && 編譯成功 通過上一步我們發(fā)現(xiàn),當(dāng)前目錄下已經(jīng)多出了幾個(gè)文件,特別是Makefile文件
第三步,測試程序 到此,第一個(gè)用cmake管理的程序,成功了!
例2: 包含目錄結(jié)構(gòu)的項(xiàng)目
在例1中完全體現(xiàn)不出cmake的任何優(yōu)勢,用g++一行可以解決的問題我們繞了一大圈??墒莄make本來的優(yōu)勢就是管理龐大的項(xiàng)目的。 這個(gè)例子用最小的程序來體現(xiàn)一個(gè)帶目錄結(jié)構(gòu)的項(xiàng)目。其中有源文件目錄,頭文件目錄。
cmake_minimum_required(VERSION 2.8.9)
project(directory_test)
#Bring the headers, such as Student.h into the project
include_directories(include)
#Can manually add the sources using the set command as follows:
#set(SOURCES src/mainapp.cpp src/Student.cpp)
#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")
add_executable(testStudent ${SOURCES})
12345678910111213
和第一個(gè)例子比起來,CMakelist.txt有如下改變:
-
使用include_directories() 包含頭文件目錄
-
使用set(SOURCES … ) 或GLOB (or GLOB_RECURSE) 設(shè)置源文件SOURCES
-
add_executable 使用變量SOURCES ,而不是具體的文件名 接下來的步驟就和例子1一樣了,不同之處是我們新建了一個(gè)build目錄來存儲(chǔ)編譯中間文件,如下圖:
下一步make,然后運(yùn)行結(jié)果如下:
例3:動(dòng)態(tài)庫編譯(.so)
有了前兩個(gè)例子的基礎(chǔ),接下來的例子我們只需要看一下目錄結(jié)構(gòu)和CMakelist.txt. CMakelist.txt如下:
project(directory_test)
set(CMAKE_BUILD_TYPE Release)
#Bring the headers, such as Student.h into the project
include_directories(include)
#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")
#Generate the shared library from the sources
add_library(testStudent SHARED ${SOURCES})
#Set the location for library installation -- i.e., /usr/lib in this case
# not really necessary in this example. Use "sudo make install" to apply
install(TARGETS testStudent DESTINATION /usr/lib)
123456789101112131415
兩個(gè)重要變化:
-
我們不再使用add_executable() 而是使用add_library()
-
install 指定安裝目錄,執(zhí)行sudo make install時(shí)動(dòng)態(tài)庫將被安裝在/usr/lib目錄 如前兩個(gè)例子,我們依次執(zhí)行,cmake make編譯結(jié)果如下:
例4:靜態(tài)庫編譯 (.a)
基于例3,我們編譯一個(gè)靜態(tài)庫 將CMakeList.txt修改為如下所示:
cmake_minimum_required(VERSION 2.8.9)
project(directory_test)
set(CMAKE_BUILD_TYPE Release)
#Bring the headers, such as Student.h into the project
include_directories(include)
#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")
#Generate the static library from the sources
add_library(testStudent STATIC ${SOURCES})
#Set the location for library installation -- i.e., /usr/lib in this case
# not really necessary in this example. Use "sudo make install" to apply
install(TARGETS testStudent DESTINATION /usr/li
12345678910111213141516
可以看出,只需將add_library中的shared改為static即可。 編譯結(jié)果如下:
例5:使用靜態(tài)庫或動(dòng)態(tài)庫
下邊我們來測試一下我們例3的結(jié)果,代碼和CMakeList.txt如下:
#include"Student.h"
int main(int argc, char *argv[]){
Student s("Joe");
s.display();
return 0;
}
1234567
cmake_minimum_required(VERSION 2.8.9)
project (TestLibrary)
#For the shared library:
set ( PROJECT_LINK_LIBS libtestStudent.so )
link_directories( ~/exploringBB/extras/cmake/studentlib_shared/build )
#For the static library:
#set ( PROJECT_LINK_LIBS libtestStudent.a )
#link_directories( ~/exploringBB/extras/cmake/studentlib_static/build )
include_directories(~/exploringBB/extras/cmake/studentlib_shared/include)
add_executable(libtest libtest.cpp)
target_link_libraries(libtest ${PROJECT_LINK_LIBS} )
123456789101112131415
結(jié)果如下(CMakeList.txt中的目錄要根據(jù)自己的情況改一下): 成功了!!
本文標(biāo)題:快速上手自動(dòng)化構(gòu)建系統(tǒng)CMake
標(biāo)題網(wǎng)址:http://m.fisionsoft.com.cn/article/dpgegee.html


咨詢
建站咨詢
