1. 程式人生 > >CMake 基本用法--寫CMakeList.txt

CMake 基本用法--寫CMakeList.txt

pac libraries argument -s .org library embedded other ppr

http://techbase.kde.org/Development/Tutorials/CMake_(zh_CN)
http://www.cmake.org/Wiki/CMake

這一章將從軟件開發人員的角度來描寫敘述怎樣有用CMake。也就是說。假設你的目標是用CMake來管理你的生成過程,請閱讀這一章。

CMake的輸入

COMMAND(args)

這裏的 COMMAND 是命令行的名稱。args是用空格切割的參數列表。典型的,對與每個項目的文件夾存在一個CMakeLists.txt。 以下我們將從一個簡單的Hello world樣例開始介紹, 它的源碼樹形文件包括這些文件:


Hello.c CMakeLists.txt

CMakeLists.txt將包括以下兩行:

PROJECT(Hello)
ADD_EXECUTABLE(Hello Hello.c)

為了生成Hello的可運行程序。你僅僅需按照上面CMake運行的過程描寫敘述來生成makefiles文件。 PROJECT 命令表明了產生的工作空間的名稱。 ADD_EXECUTABLE命令加入可運行的目標到生成程序。這個簡單的程序就僅僅須要這些設置。

如歌你的項目須要一些文件才幹編譯也非常easy,僅僅想改動ADD_EXECUTABLE命令行例如以下:

ADD_EXECUTABLE(Hello Hello.c File2.c File3.c File4.c)

ADD_EXECUTABLE僅僅是非常多CMake命令中的一種。比方更復雜的例如以下:

PROJECT(HELLO)
SET(HELLO_SRCS Hell.c File2.c File3.c)
IF(WIN32)
SET(HELLO_SRCS ${HELLO_SRCS} WinSupport.c)
ELSE (WIN32)
SET(HELLO_SRCS ${HELLO_SRCS} UnixSupport.c)
ENDIF (WIN32)

ADD_EXECUTABLE (Hello ${HELLO_SRCS})

#look for the Tcl library
FIND_LIBRARY(TCL_LIBRARY NAMES tcl tc184 tc183 tc 182 tc 180
PATHS /usr/lib /usr/local/lib)
IF (TCL_LIBRARY)
TARGET_ADD_LIBRARY (Hello TCL_LIBRARY)
ENDIF(TCL_LIBRARY)

在這個樣例中 SET 命令用於將源文件組成一個列表。

IF 命令用於添加WinSupport.c或者UnixSupport.c到列表中。 最後 ADD_EXECUTABLE 命令用於 採用源文件列表HELLO_SRCS中列出的文件 生成可運行文件。FIND_LIBRARY命令用於尋找在一些指定文件夾下的特定的Tcl庫文件。

假設找到了,就將他們加入到Hello可運行程序的鏈接命令。 #行為凝視行。



CMake 是會定義一些使用的變量在CMakeList文件裏。 比方。WIN32總是會在windows系統中被定義。而UNIX
總是在UNIX系統中被定義。

生成目標:(Build Targets)

SET()
SUBDIRS()
ADD_LIBRARY()
這裏生成靜態鏈接文件,比如ADD_LIBRARY(Whole ${HELLO_SRC}),就會生成一個libWhole.a可供鏈接

ADD_EXECUTABLE()
AUX_SOURCE_DIRECTORY()
PROJECT()

CMake會循環的查找從當前文件夾到SUBDIRS列出的不論什麽子文件夾的文件。SET命令用於設定一個變量。ADD_LIBRARY將加入一個庫到目標之中。 ADD_EXECUTABLE加入一個可運行程序到目標列表中。(Note:編譯器運行的順序是先編譯源文件,然後生成庫文件,最後生成可運行文件)。

AUX_SOURCE_DIRECTORY表示一個不在當前文件夾的包括源文件的文件夾。

這些源碼將插入當前的庫(LIBRARY)中。全部在AUX_SOURCE_DIRECTORY的文件將被編譯(如,*.c,*.cxx,*.cpp等等)。PROJECT(ProjectName)是一個用在MSVC中的特殊變量。用於為編譯器生成項目。

他也為CMAKE定義連個實用的變量:ProjectName_SOURCE_DIR和ProjectName_BINARY_DIR.

編譯的標示和選項。

除了上面列出的命令外。CMakeLists.txt還包括例如以下的命令:
INCLUDE_DIRECTORIES()
LINK_DIRECTORIES()
LINK_LIBRARIES()
TARGET_LINK_LIBRARIES()

這些命令定義了用於編譯源碼和生成可運行程序的文件夾和庫。

上面列出的文件夾的一個非常重要的特性是它們會被不論什麽子文件夾繼承。也就是說。CMake按照文件夾的分層結構來承襲這些命令。在每次遇到對這些命令的描寫敘述的時候都會被展開一次。比方說。假設在頂層的CMakeLists文件裏有定義INCLUDE_DIRECTORIES(/usr/include)和SUBDIRS(./subdir1),而且在./subdir1/CMakeLists.txt有INCLUDE_DIRECTORIES(/tmp/foobar),於是最後網狀的結果是
INCLUDE_DIRECTORIES(/usr/include /tmp/foobar)

CMake會定義很多的模塊來查找一般會用到的包。比方OpenGL或Java。 這些模塊為你節省了非常多的時間來編寫這些查找包。這些模塊能夠像這樣加到你的CMakeList文件裏。例如以下:

INCLUDE(${CMAKE_ROOT}/Modules/FindTCL.cmake)

CMAKE_ROOT 總是定義在CMake中,用於指向CMake安裝的路徑。查看Modules子文件夾下的一些文件能夠給你提供一些非常好的idea關於如何用這些CMake命令。

給項目文件加入一個新的文件夾
一個通用的方法來擴展一個項目文件是給他加入一個新的目錄。這將包括三個步驟:
1.創建一個新的文件夾在你的源碼的分層文件夾中
2.將這個新的文件夾加入到SUBDIRS命令中
3.在這個新創建的文件夾中用適當的命令建立一個CMakeLists.txt文件

This section describes how to use CMake from the software developer‘s point of view. That is, if your aim is to use CMake to manage your build process, read this section first.
Input to CMake

COMMAND(args)
Where COMMAND is the name of the command, and args is a white-space separated list of arguments to the command. (Arguments with embedded white-space should be quoted.) Typically there will be a CMakeLists.txt file for each directory of the project. Let‘s start with a simple example. Consider building hello world. You would have a source tree with the following files:
Hello.c CMakeLists.txt
The CMakeLists.txt file would contain two lines:
PROJECT (Hello)
ADD_EXECUTABLE(Hello Hello.c)
To build the Hello executable you just follow the process described in Running CMake above to generate the makefiles or Microsoft project files. The PROJECT command indicates what the name of the resulting workspace should be and the ADD_EXECUTABLE command adds an executable target to the build process. That‘s all there is to it for this simple example. If your project requires a few files it is also quite easy, just modify the ADD_EXECUTABLE line as shown below.
ADD_EXECUTABLE(Hello Hello.c File2.c File3.c File4.c)

ADD_EXECUTABLE is just one of many commands available in CMake. Consider the more complicated example below.

PROJECT (HELLO)
SET(HELLO_SRCS Hello.c File2.c File3.c)
IF (WIN32)
SET(HELLO_SRCS ${HELLO_SRCS} WinSupport.c)
ELSE (WIN32)
SET(HELLO_SRCS ${HELLO_SRCS} UnixSupport.c)
ENDIF (WIN32)
ADD_EXECUTABLE (Hello ${HELLO_SRCS})

# look for the Tcl library
FIND_LIBRARY(TCL_LIBRARY NAMES tcl tcl84 tcl83 tcl82 tcl80
PATHS /usr/lib /usr/local/lib)
IF (TCL_LIBRARY)
TARGET_ADD_LIBRARY (Hello TCL_LIBRARY)
ENDIF (TCL_LIBRARY)
In this example the SET command is used to group together source files into a list. The IF command is used to add either WinSupport.c or UnixSupport.c to this list. And finally the ADD_EXECUTABLE command is used to build the executable with the files listed in the source list HELLO_SRCS. The FIND_LIBRARY command looks for the Tcl library under a few different names and in a few different paths, and if it is found adds it to the link line for the Hello executable target. Note the use of the # character to denote a comment line.
CMake always defines some variables for use within CMakeList files. For example, WIN32 is always defined on windows systems and UNIX is always defined for UNIX systems. CMake defines a number of commands. A brief summary of the most commonly used commands follows here. Later in the document an exhaustive list of all pre-defined commands is presented. (You may also add your own commands, see the Extension Guide for more information.)

Build Targets:


SET()
SUBDIRS()
ADD_LIBRARY()
這裏生成靜態鏈接文件。比如ADD_LIBRARY(Whole ${HELLO_SRC}),就會生成一個libWhole.a。可供鏈接。



ADD_EXECUTABLE()
AUX_SOURCE_DIRECTORY()
PROJECT()
CMake works recursively, descending from the current directory into any subdirectories listed in the SUBDIRS command. The command SET is used for setting a variable, in this case to a list of source files. (Note: currently only C and C++ code can be compiled.) ADD_LIBRARY adds a library to the list of targets this makefile will produce. ADD_EXECUTABLE adds an executable to the list of targets this makefile will produce. (Note: source code is compiled first, then libraries are built, and then executables are created.) The AUX_SOURCE_DIRECTORY is a directory where other source code, not in this directory, whose object code is to be inserted into the current LIBRARY. All source files in the AUX_SOURCE_DIRECTORY are compiled (e.g. *.c, *.cxx, *.cpp, etc.). PROJECT (PojectName) is a special variable used in the MSVC to create the project for the compiler, it also defines two useful variables for CMAKE: ProjectName_SOURCE_DIR and ProjectName_BINARY_DIR.

Build flags and options. In addition to the commands listed above, CMakeLists.txt often contain the following commands:

INCLUDE_DIRECTORIES()
LINK_DIRECTORIES()
LINK_LIBRARIES()
TARGET_LINK_LIBRARIES()
These commands define directories and libraries used to compile source code and build executables. An important feature of the commands listed above is that are inherited by any subdirectories. That is, as CMake descends through a directory hierarchy (defined by SUBDIRS()) these commands are expanded each time a definition for a command is encountered. For example, if in the top-level CMakeLists file has INCLUDE_DIRECTORIES(/usr/include), with SUBDIRS(./subdir1), and the file ./subdir1/CMakeLists.txt has INCLUDE_DIRECTORIES(/tmp/foobar), then the net result is
INCLUDE_DIRECTORIES(/usr/include /tmp/foobar)
CMake comes with a number of modules that look for commonly used packages such as OpenGL or Java. These modules save you from having to write all the CMake code to find these packages yourself. Modules can be used by including them into your CMakeList file as shown below.

INCLUDE (${CMAKE_ROOT}/Modules/FindTCL.cmake)
CMAKE_ROOT is always defined in CMake and can be used to point to where CMake was installed. Looking through some of the files in the Modules subdirectory can provide good ideas on how to use some of the CMake commands.

Adding A New Directory to a project

A common way to extend a project is to add a new directory. This involves three steps:
Create the new directory somewhere in your source directory hierarchy.
Add the new directory to the SUBDIRS command in the parent directories CMakeLists.txt
Create a CMakeLists.txt in the new directory with the appropriate commands

CMake 基本用法--寫CMakeList.txt