1、CMakeLists 编写CMake简介CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C+特性,类似UNIX下的automake。CMake 使用方法CMake的所有的语句都写在一个叫:CMakeLists.txt的文件中。当CMakeLists.txt文件确定后,可以用ccmake命令对相关 的变量值进行配置。这个命令必须指向CMakeLists.txt所在的目录。配置完成之后,应用cmake命令生成相应的makefile(在Unix like系统下)或者 proje
2、ct文件(指定用window下的相应编程工具编译时)。其基本操作流程为:$ ccmake directory$ cmake directory$ make其中directory为CMakeList.txt所在目录;第一条语句用于配置编译选项,如VTK_DIR目录 ,一般这一步不需要配置,直接执行第二条语句即可,但当出现错误时,这里就需要认为配置了,这一步才真正派上用场;第二条命令用于根据CMakeLists.txt生成Makefile文件;第三条命令用于执行Makefile文件,编译程序,生成可执行文件;CMake的执行就是这么简单,其难点在于如何编写CMakeLists.txt文件,下面结合
3、例子简单介绍CMakeLists.txt的编写,看下面这个CMakeLists.txt#project namePROJECT(test_math)#head file pathINCLUDE_DIRECTORIES(include)#source directoryAUX_SOURCE_DIRECTORY(src DIR_SRCS)#set environment variableSET(TEST_MATH$DIR_SRCS)#set extern librariesSET(LIBRARIESlibm.so)#add executable fileADD_EXECUTABLE(./bin/b
4、in $TEST_MATH)#add link libraryTARGET_LINK_LIBRARIES(./bin/bin $LIBRARIES)或者用下面这个CMakeLists.txt#project namePROJECT(test_math)#head file pathINCLUDE_DIRECTORIES(include)#source directoryAUX_SOURCE_DIRECTORY(src DIR_SRCS)#set environment variableSET(TEST_MATH$DIR_SRCS)#add executable fileADD_EXECUTAB
5、LE(./bin/bin $TEST_MATH)#add link libraryTARGET_LINK_LIBRARIES(./bin/binm)这是一个测试数学函数的程序的CMakeLists.txt,#后面为注释的内容,CMake的命令全部为大写第2行指定生成的工程名为test_math第4行指定头文件目录为include第8行指定源文件目录为src,并将其赋值给环境变量DIR_SRCS第10行设定环境变量TEST_MATH的值为环境变量DIR_SRCS的值,此处用于显示如何用环境变量对环境变量进行赋值第14行将数学函数库赋值给环境变量LIBRARIES,当然,可以不用这个环境变量,而在
6、后面直接使用该库名第18行用于指定生成文件,将环境变量TEST_MATH目录下的所有文件编译生成./bin目录下的可执行文件bin第20行指定./bin/bin执行时的链接库为环境变量LIBRARIES的值libm.so下面给出源文件/src/main.c:#include#include./include/a.hintmain()doubleb=25.0;doublea=0.0;a=get_sqrt(b);printf(a is %lf, b is %lfn,a,b);return0;/src/a.c#include./include/a.hdoubleget_sqrt(doublevar1
7、)returnsqrt(var1);/include/a.h#ifndef A_FILE_HEADER_INC#define A_FILE_HEADER_INC#includedoubleget_sqrt(doublevar1);#endif将CMakeLists.txt放在当前目录下,执行CMakeLists.txt$ cmake .$ make即可生成可执行文件,在目录/bin下的bin文件,好了运行看其效果是否和所想一样。这一章将从软件开发者的角度来描述如何实用CMake。也就是说,如果你的目标是用CMake来管理你的生成过程,请阅读这一章。CMake的输入COMMAND(args)这里
8、的 COMMAND 是命令行的名称,args是用空格分割的参数列表。典型的,对与每一个项目的目录存在一个CMakeLists.txt。下面我们将从一个简单的Hello world例子开始介绍, 它的源代码树形文件包含这些文件:Hello.c CMakeLists.txtCMakeLists.txt将包含下面两行:PROJECT(Hello)ADD_EXECUTABLE(Hello Hello.c)为了生成Hello的可执行程序,你只需依照上面CMake运行的过程描述来生成makefiles文件。 PROJECT 命令表明了产生的工作空间的名称。 ADD_EXECUTABLE命令添加可执行的目标
9、到生成程序。这个简单的程序就只需要这些设置。如歌你的项目需要一些文件才能编译也很容易,只想修改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 U
10、nixSupport.c)ENDIF (WIN32)ADD_EXECUTABLE (Hello $HELLO_SRCS)#look for the Tcl libraryFIND_LIBRARY(TCL_LIBRARY NAMES tcl tc184 tc183 tc 182 tc 180PATHS /usr/lib /usr/local/lib)IF (TCL_LIBRARY)TARGET_ADD_LIBRARY (Hello TCL_LIBRARY)ENDIF(TCL_LIBRARY)在这个例子中 SET 命令用于将源文件组成一个列表。 IF 命令用于增加WinSupport.c或者Uni
11、xSupport.c到列表中。 最后 ADD_EXECUTABLE 命令用于采用源文件列表HELLO_SRCS中列出的文件生成可执行文件。FIND_LIBRARY命令用于寻找在一些指定目录下的特定的Tcl库文件。如果找到了,就将他们添加到Hello可执行程序的链接命令。 #行为注释行。CMake 是会定义一些使用的变量在CMakeList文件中。比如,WIN32总是会在windows系统中被定义,而UNIX总是在UNIX系统中被定义。生成目标:(Build Targets)SET()SUBDIRS()ADD_LIBRARY()这里生成静态链接文件,例如ADD_LIBRARY(Whole $HE
12、LLO_SRC),就会生成一个libWhole.a可供链接ADD_EXECUTABLE()AUX_SOURCE_DIRECTORY()PROJECT()CMake会循环的查找从当前目录到SUBDIRS列出的任何子目录的文件。SET命令用于设定一个变量。ADD_LIBRARY将添加一个库到目标之中。 ADD_EXECUTABLE添加一个可执行程序到目标列表中。(Note:编译器执行的顺序是先编译源文件,然后生成库文件,最后生成可执行文件)。AUX_SOURCE_DIRECTORY表示一个不在当前目录的包含源文件的目录。这些源代码将插入当前的库(LIBRARY)中。所有在AUX_SOURCE_DI
13、RECTORY的文件将被编译(如,*.c,*.cxx,*.cpp等等)。PROJECT(ProjectName)是一个用在MSVC中的特殊变量,用于为编译器生成项目。他也为CMAKE定义连个有用的变量:ProjectName_SOURCE_DIR和ProjectName_BINARY_DIR.编译的标示和选项。除了上面列出的命令外,CMakeLists.txt还包含如下的命令:INCLUDE_DIRECTORIES()LINK_DIRECTORIES()LINK_LIBRARIES()TARGET_LINK_LIBRARIES()这些命令定义了用于编译源代码和生成可执行程序的目录和库。上面列出
14、的目录的一个很重要的特性是它们会被任何子目录继承。也就是说,CMake依照目录的分层结构来承袭这些命令。在每次遇到对这些命令的描述的时候都会被展开一次。比如说,如果在顶层的CMakeLists文件中有定义INCLUDE_DIRECTORIES(/usr/include)和SUBDIRS(./subdir1),并且在./subdir1/CMakeLists.txt有INCLUDE_DIRECTORIES(/tmp/foobar),于是最后网状的结果是INCLUDE_DIRECTORIES(/usr/include /tmp/foobar)CMake会定义许多的模块来查找通常会用到的包,比如Ope
15、nGL或Java。这些模块为你节省了很多的时间来编写这些查找包。这些模块可以像这样加到你的CMakeList文件中,如下:INCLUDE($CMAKE_ROOT/Modules/FindTCL.cmake)CMAKE_ROOT 总是定义在CMake中,用于指向CMake安装的路径。查看Modules子目录下的一些文件可以给你提供一些很好的idea关于怎样用这些CMake命令。给项目文件添加一个新的目录一个通用的方法来扩展一个项目文件是给他添加一个新的文件夹。这将包含三个步骤:1.创建一个新的目录在你的源代码的分层目录中2.将这个新的目录添加到SUBDIRS命令中3.在这个新创建的目录中用适当的
16、命令建立一个CMakeLists.txt文件This section describes how to use CMake from the software developers point of view. That is, if your aim is to use CMake to manage your build process, read this section first.Input to CMakeCOMMAND(args)Where COMMAND is the name of the command, and args is a white-space separate
17、d 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. Lets start with a simple example. Consider building hello world. You would have a source tree with the following files:Hello.cC
18、MakeLists.txtThe 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
19、 resulting workspace should be and the ADD_EXECUTABLE command adds an executable target to the build process. Thats 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
20、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 (WIN3
21、2)ADD_EXECUTABLE (Hello $HELLO_SRCS)# look for the Tcl libraryFIND_LIBRARY(TCL_LIBRARY NAMES tcl tcl84 tcl83 tcl82 tcl80PATHS/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
22、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 f
23、ew 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 def
24、ined 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 Targ
25、ets: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 v
26、ariable, 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 fir
27、st, 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, et
28、c.). 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 t
29、he 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 i
30、s, 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
31、.txt has INCLUDE_DIRECTORIES(/tmp/foobar), then the net result isINCLUDE_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 alway
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1