大家好,如果您還對cmake使用方法詳解不太了解,沒有關系,今天就由本站為大家分享cmake使用方法詳解的知識,包括cmake中文手冊pdf的問題都會給大家分析到,還望可以解決大家的問題,下面我們就開始吧!
github的源代碼cmakelists怎么使用
./configure就是執行你當前目錄下一個名叫configure的腳本,由它生成Makefile,有了Makefile之后,一般來說就可以通過make進行編譯,makeinstall進行安裝cmake就是一個與make同級別的編譯工具,只不過它依靠的不是Makefile作為編譯規則
如何使用CMAKE生成makefile文件
CMake是一個跨平臺的安裝(編譯)工具,可以用簡單的語句來描述所有平臺的安裝(編譯過程)。他能夠輸出各種各樣的makefile或者project文件,能測試編譯器所支持的C++特性。只是CMake的組態檔取名為CmakeLists.txt。Cmake并不直接建構出最終的軟件,而是產生標準的建構檔(如linux的Makefile或WindowsVisualC++的projects/workspaces),然后再依一般的建構方式使用。
在linux平臺下使用CMake生成Makefile并編譯的流程如下:
編寫CmakeLists.txt。
執行命令“cmakePATH”或者“ccmakePATH”生成Makefile(PATH是CMakeLists.txt所在的目錄)。
使用make命令進行編譯
工程實例:
一.編寫各層CMakeLists.txt
主目錄的主程序main.cpp
#include"hello.h"
externHellohello;
intmain()
{
hello.Print();
return0;
}
主目錄的CMakeLists.txt
#totherootbinarydirectoryoftheprojectas${MAIN_BINARY_DIR}.
project(MAIN)
#versionsupport
cmake_minimum_required(VERSION2.8)
#Recurseintothe"Hello"and"Demo"subdirectories.Thisdoesnotactually
#causeanothercmakeexecutabletorun.Thesameprocesswillwalkthrough
#theproject'sentiredirectorystructure.
add_subdirectory(Hello)
add_subdirectory(Demo)
#MakesurethecompilercanfindincludefilesfromourHellolibrary.
include_directories(${MAIN_SOURCE_DIR}/Hello)
#MakesurethelinkercanfindtheHelloDemolibraryonceitisbuilt.
link_directories(${HELLO_BINARY_DIR}/Hello)
link_directories(${HELLO_BINARY_DIR}/Demo)
#definethesourcecoedesofcurrentdirectoryasDIR_SRCS
AUX_SOURCE_DIRECTORY(.DIR_SRCS)
#Addexecutablecalled"MAIN"thatisbuiltfromthesourcefiles
add_executable(Main${DIR_SRCS})
#LinktheexecutabletotheHelloDemolibrary.
target_link_libraries(MainHelloDemo)
定義項目名project(MAIN),使得當前目錄可以用${MAIN_SOURCE_DIR},由于有2個子目錄,所以需要add_subdirectory它們。由于主程序會使用到其他庫,因而也需要指定連接庫所在目錄。
主目錄下的作用是利用add_executable將當前目錄下的源文件編譯成Main程序,然后通過target_link_libraries鏈接Hello和Demo庫。由于主程序文件使用了hello.h文件,所以要include_directories該目錄。
---------------------------------------------------------------------------------------------------
子目錄Demo的子程序demo.c
#include"hello.h"
Hellohello;
子目錄Demo的CMakeLists.txt
#MakesurethecompilercanfindincludefilesfromourHellolibrary.
include_directories(${MAIN_SOURCE_DIR}/Hello)
#definethesourcecoedesofcurrentdirectoryasDIR_DEMO_SRCS
AUX_SOURCE_DIRECTORY(.DIR_DEMO_SRCS)
#Addlibrarycalled"Demo"thatisbuiltfromthesourcefiles
add_library(Demo${DIR_DEMO_SRCS})
Demo目錄下的CMakeLists主要作用是利用add_library將當前目錄源碼編譯成Demo庫,由于該庫使用到hello.h文件,所以要include_directories該目錄。
---------------------------------------------------------------------------------------------------
子目錄Hello的子程序hello.h
#ifndef_hello_h
#define_hello_h
classHello
{
public:
voidPrint();
};
#endif
子目錄Hello的子程序hello.c
#include"hello.h"
#include
voidHello::Print()
{
printf("Hello,World!\n");
}
子目錄Hello的CMakeLists.txt
#definethesourcecoedesofcurrentdirectoryasDIR_HELLO_SRCS
AUX_SOURCE_DIRECTORY(.DIR_HELLO_SRCS)
#Addlibrarycalled"hello"thatisbuiltfromthesourcefiles
add_library(Hello${DIR_HELLO_SRCS})
Hello目錄下的CMakeLists主要作用是利用add_library將當前目錄源碼編譯成Hello庫。
---------------------------------------------------------------------------------------------------
二.執行cmake命令
至此我們完成了項目中所有CMakeLists.txt文件的編寫,進入目錄step2中依次執行命令
#cmake.
默認當前目錄,生產makefile
#make
最后編譯程序
cmake使用方法詳解的介紹就聊到這里吧,感謝你花時間閱讀本站內容,更多關于cmake中文手冊pdf、cmake使用方法詳解的信息別忘了在本站進行查找哦。