最近,公司的代码编译从直接使用 GNU make 转向了 cmake.
下面介绍一下cmake 的使用
1) cmake 的基本语法
cmake 可以看作一种脚本语言,它的一些语法和其他一下脚本语言(bash, python等)是相似的。
1.1 命令:
基本格式为
command(param)
命令可大写或者小写 (case-insensitive)
1.1 变量
引用变量 var的值: ${var}
给变量赋值:
set(Foo a b c)
或者
set(Foo a;b;c)
Foo 的值为 a b c (一个 list)
$ENV{name} --》得到环境变量的值
2. 流程控制:
2.1 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)
2. loop:
set(VAR a b c) # loop over a, b,c with the variable f foreach(f ${VAR}) message(${f}) endforeach(f)3. 宏和函数
# 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)
下面举个例子来说明如何使用 cmake.
下面的例子中, 我们用 cmake 来生成静态库,动态库, 并使用生成的库文件.
代码树如下:
. ├── CMakeLists.txt ├── FooFunctions │ ├── CMakeLists.txt │ ├── inc │ │ └── foo.h │ └── src │ └── foo.c ├── MathFunctions │ ├── CMakeLists.txt │ ├── inc │ │ └── mysqrt.h │ └── src │ └── mysqrt.c └── tutorial.ctutorial.c 是测试代码, FooFunctions 下的文件生成共享库, mathFunctions 下的代码生成静态库.
top-level CMakeList.txt 如下:
cmake_minimum_required (VERSION 2.6) 2 project (Tutorial) 3 4 set(CMAKE_BUILD_TYPE Debug) 5 6 #message(status "PROJECT_SOURCE_DIR = ${PROJECT_SOURCE_DIR}") 7 #message(status "PROJECT_BINARY_DIR = ${PROJECT_BINARY_DIR}") 8 #message(status "CMAKE_SOURCE_DIR = ${CMAKE_SOURCE_DIR}") 9 #message(status "CMAKE_BINARY_DIR = ${CMAKE_BINARY_DIR}") 10 11 include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions/inc") 12 include_directories("${PROJECT_SOURCE_DIR}/FooFunctions/inc") 13 add_subdirectory (MathFunctions) 14 add_subdirectory(FooFunctions) 15 16 add_executable(Tutorial tutorial.c) 17 target_link_libraries (Tutorial MathFunctions m FooFunctions) 18 set_target_properties(Tutorial PROPERTIES RUNTIME_OUTPUT_DIRECTORY ../BIN)
解释如下:
1) set(CMAKE_BUILD_TYPE Debug)
设置debug模式,也就是 gcc 加上 -g 选项
也可以用
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-g -Wall")
或者命令行设置:
cmake -DCMAKE_BUILD_TYPE=Debug/Release dir
2) include_directories
设置头文件的搜索路径. 相当于 C_FLAGS 加上 "-I"
3)add_executable
设置生成的可执行文件
4) add_subdirectory
设置包含 其它 CMakeLists.txt的目录.
一般,需要单独变成一个库的代码,都要有一个 CMakeLists.txt
5)target_link_libraries
设置要 link到可执行文件的库
6)set_target_properties
这里是设置可执行文件的位置
然后再看一下 FooFunctions 下的 CMakeLists.txt:
set(shared_src src/foo.c) 2 add_library(FooFunctions SHARED ${shared_src}) 3 include_directories("${PROJECT_SOURCE_DIR}/FooFunctions/inc") 4 set_target_properties(FooFunctions PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/LIB) 5 set_target_properties(FooFunctions PROPERTIES VERSION 1.1 SOVERSION 1)第二行是设置库为共享库, 最后一行设置生成的共享库版本:VERSION: build version; SOVERSION: API version
比如,最后生成的共享库如下:
$ ls LIB/ -l total 12 lrwxrwxrwx 1 charles charles 20 Feb 20 01:38 libFooFunctions.so -> libFooFunctions.so.1 lrwxrwxrwx 1 charles charles 22 Feb 20 01:38 libFooFunctions.so.1 -> libFooFunctions.so.1.1 -rwxrwxr-x 1 charles charles 7726 Feb 20 01:38 libFooFunctions.so.1.1
1 add_library(MathFunctions src/mysqrt.c) 2 set_target_properties(MathFunctions PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CM AKE_BINARY_DIR}/LIB)第一行是设置静态库
每一个 CMakeLists.txt 都会生成一组 Makefile文件.
其中主要的是 Makefile, link.txt 和 flags.make
link.txt 是用来生成可执行文件或者库的命令. flags.make 则是命令选项.
比如, top-level CMakeLists.txt 的 link.txt 为:
charles@mikiehara:~/Code/cmake_tutorial/cmake/CMakeFiles/Tutorial.dir$ cat link.txt /usr/bin/gcc -g CMakeFiles/Tutorial.dir/tutorial.c.o -o ../BIN/Tutorial -rdynamic LIB/libMathFunctions.a -lm LIB/libFooFunctions.so.1.1 -Wl,-rpath,/home/charles/Code/cmake_tutorial/cmake/LIBflags.make 为:
charles@mikiehara:~/Code/cmake_tutorial/cmake/CMakeFiles/Tutorial.dir$ cat flags.make # CMAKE generated file: DO NOT EDIT! # Generated by "Unix Makefiles" Generator, CMake Version 2.8 # compile C with /usr/bin/gcc C_FLAGS = -g -I/home/charles/Code/cmake_tutorial/MathFunctions/inc -I/home/charles/Code/cmake_tutorial/FooFunctions/inc C_DEFINES =