# CMake 基本使用方法--写CMakeList.txt

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

CMake的输入

COMMAND(args)

Hello.c CMakeLists.txt

CMakeLists.txt将包含下面两行：

PROJECT（Hello)

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可供链接

AUX_SOURCE_DIRECTORY()
PROJECT()

INCLUDE_DIRECTORIES()

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)
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(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)