做第一个linux项目时,Makefile是一行行敲入的,第二个项目后,开始使用cmake。至于为何选择cmake,倒不是觉得它有什么好,仅仅是因为当时项目组中的一个linux前辈向我们推荐了这个。经过一番研究之后,并在项目中使用,现将使用经验总结一下,供大家参考。
学习一项新知识的时候,最好是从sample开始。cmake官方网站就给出了一个简单的例子。在开始之前,还是先安装cmake程序,在ubuntu下非常简单,输入以下命令即可:
$
sudo apt-get install cmake
下面来看看sample中的内容。sample包含一个主目录,下面两个子目录: Demo和Hello。其中Hello包含程序库代码,Demo中为可执行程序,需要连接Hello中的程序库。总共有三个CMakeLists.txt文件,每个目录下一个。主目录下的CMakeLists.txt文件内容为:
# The name of our project is "HELLO". CMakeLists files in this project can # refer to the root source directory of the project as ${HELLO_SOURCE_DIR} and # to the root binary directory of the project as ${HELLO_BINARY_DIR}. project (HELLO) # Recurse into the "Hello" and "Demo" subdirectories. This does not actually # cause another cmake executable to run. The same process will walk through # the project's entire directory structure. add_subdirectory (Hello) add_subdirectory (Demo)
Hello目录下的CMakeLists.txt文件为:
# Create a library called "Hello" which includes the source file "hello.cxx". # The extension is already found. Any number of sources could be listed here. add_library (Hello hello.cxx)
最后,Demo包含如下CMakeLists.txt文件:
# Make sure the compiler can find include files from our Hello library. include_directories (${HELLO_SOURCE_DIR}/Hello) # Make sure the linker can find the Hello library once it is built. link_directories (${HELLO_BINARY_DIR}/Hello) # Add executable called "helloDemo" that is built from the source files # "demo.cxx" and "demo_b.cxx". The extensions are automatically found. add_executable (helloDemo demo.cxx demo_b.cxx) # Link the executable to the Hello library. target_link_libraries (helloDemo Hello)
CMake在主目录执行时,会处理该目录下CMakeLists.txt文件,然后进入到子目录,处理子目录下的CMakeLists.txt.
从字面上看,我们差不多可以理解这三个文件的涵义。第一个CMakeLists.txt文件指定包含Hello和Demo两个子目录。第二个文件则指定生成Hello库文件,第三个文件则是生成一个可执行文件helloDemo,另外两个附加语句则用来指明头文件路径以及所要链接的库。
乍一看,为了编译一个程序,需要写三个CMakeLists.txt文件,相对于原来只需写一个Makefile文件,不是更复杂了吗?事实上不尽然,虽然要写三个CMakeLists文件,但每个文件都非常简单,总共算起来,并不比一个Makefile文件多多少。更重要的是,这其中隐含着linux哲学:分而治之。每个模块自行编写配置文件,只负责自己份内的事务,所以可扩展性好。在进行大型项目开发,就可以体现出优势了。
CMakeLists.txt相当于定义了一套生成Makefile文件的规则,下面就可以生成Makefile文件了,命令如下:
$
cmake .
注:.表示当前目录,如果CMakeLists.txt不在当前目录,请在cmake后面指定。在主目录下和Demo、Hello子目录下均会生成一个Makefile文件,有了这个文件,我们就可以敲入make编译目标程序了。
总结起来,使用CMake编译应用程序的流程为:

CMake语法非常简单,包含注释、命令和空格。以#开头的行为注释行,命令则由命令名、括号及以空格进行分隔的参数组成。命令可以是诸如add_library这样的内置命令,也可以是子定义的宏或者函数。CMake的输入是主目录下的CMakeLists.txt文件,该文件可以使用include或者add_directory命令添加其它的输入文件。
命令的形式如下:
command (args ...)
其中<I>command</I>为命令名,<B>args</B>为空格分隔的参数列表,如果参数中包含空格,使用双引号引起来。命令不区分大小写。
lists and strings. CMake的基本数据类型为字符串,字符串又可以组成list类型,有两种方式:一种通过分号分隔,一种通过空格分隔。比如以下例子给VAR赋了同样的值:
set(VAR a;b;c) set(VAR a b c)
字符串列表主要用于foreach进行迭代,有些命令也用于对list进行处理。
CMake支持字符串和list类型的简单变量,变量以${VAR}形式引用。多个参数可以用set命令组成一个list,命令将展开list,例如:
set(Foo a b c) command(${Foo})
等价于
command(a b c)
如果你希望将list当作一个参数传递给命令,就应该用双引号把list引起来,如command("${Foo}")等价于command("a b c")
写CMakeLists.txt文件就象写一个简单的程序,CMake提供了三种流程控制结构:
- 条件语句if
# some_command will be called if the variable's value is not: # empty, 0, N, NO, OFF, FALSE, NOTFOUND, or -NOTFOUND. if(var) some_command(...) endif(var)
- 循环结构
set(VAR a b c) # loop over a, b,c with the variable f foreach(f ${VAR}) some_command(${f}) endforeach(f)
- 宏和函数,函数在2.6及以上版本才支持,函数和宏的区别在于函数中可定义局部变量,而宏定义的变量都是全局变量。
# define a macro hello macro(hello MESSAGE) message(${MESSAGE}) endmacro(hello) # call the macro with the string "hello world" hello("hello world") # define a function hello function(hello MESSAGE) message(${MESSAGE}) endfunction(hello)