版权信息:可以任意转载, 转载时请务必以超链接形式标明文章原文出处 ,谢谢
原文出处: http://brianlicn.appspot.com/2010/07/11/how_to_use_cmake.html
引言
在项目比较小的时候,只需要简单地编写Makefile文件就可以完成了对项目的管理。随着项目的规模日益增大,使用一个好的构建工具来管理项目, 变得非常重要。过去一般是使用autoconf, automake那一套来解决,即./configure, make, make install。由于自己对autoconf不是很懂也不感冒,所以没有怎么去用它。最近在看到了CMake,这是一个夸平台的项目构建工具,很简单,夸 平台,对我来说,能够简单地上手并且解决所需要的需求。所以写下这个CMake简单使用的博客,记录下来方便将来查阅。
项目简介
首先给出项目的一个布局(layout)吧。
brianli@sunrise:/opt/projects/ttiger$ tree . |-- CMakeLists.txt |-- build |-- include | `-- ttiger | |-- exception | | `-- exception.hpp | `-- util | `-- test.hpp |-- src | |-- CMakeLists.txt | `-- ttiger | |-- exception | | `-- exception.cxx | `-- util | `-- test.cxx |-- tests | |-- CMakeLists.txt | `-- mytest.cxx `-- unittests |-- CMakeLists.txt `-- ttiger_unittests.cxx
可以看到,在这个项目是一个以ttiger库为主体的项目。
CMakeLists示例
上面简单地介绍了项目的布局,那么下面就记录下项目中应用到的CMakeList.txt
根目录
因为CMakeLists.txt是嵌套的,下面先介绍最上面的
# the whole CMakeLists.txt for project ttiger PROJECT(ttiger) CMAKE_MINIMUM_REQUIRED(VERSION 2.4) # include directories INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/include/ttiger /usr/local/include /usr/include ) # lib directories LINK_DIRECTORIES( ${PROJECT_BINARY_DIR}/lib /usr/local/lib /use/lib ) SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) ADD_SUBDIRECTORY(src) ADD_SUBDIRECTORY(tests) ADD_SUBDIRECTORY(unittests)
SRC目录
下面的是src下面的CMakeLists.txt文件:
# CMakeLists for src directory PROJECT(ttiger) SET(TTIGER_SRCS ttiger/exception/exception.cxx ttiger/util/test.cxx ) # shared library ADD_LIBRARY(ttiger SHARED ${TTIGER_SRCS}) # static library ADD_LIBRARY(ttiger_static STATIC ${TTIGER_SRCS}) SET_TARGET_PROPERTIES(ttiger_static PROPERTIES OUTPUT_NAME "ttiger") SET_TARGET_PROPERTIES(ttiger PROPERTIES CLEAN_DIRECT_OUTPUT 1) SET_TARGET_PROPERTIES(ttiger_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
应用程序目录
下面给出使用了ttiger库的应用程序的CMakeLists.txt文件
# test directory CMakeLists.txt ADD_EXECUTABLE(mytest mytest.cxx) TARGET_LINK_LIBRARIES(mytest ttiger)
其就是用ADD_EXECUTABLE来告诉编译啥可执行文件,用TARGET_LINK_LIBRARIES来指定使用了那些库,这样就可以编译 出来mytest可执行文件了
单元测试目录
# CMakeLists.txt for directory unittests ADD_EXECUTABLE(ttiger_unittests ttiger_unittests.cxx) TARGET_LINK_LIBRARIES(ttiger_unittests ttiger;gtest;pthread
具体情况跟应用程序目录一样
应用程序编译与执行
所有的都已经写好了,开始编译吧
brianli@sunrise:/opt/projects/ttiger$ cd build/ brianli@sunrise:/opt/projects/ttiger/build$ cmake .. -- The C compiler identification is GNU -- The CXX compiler identification is GNU -- Check for working C compiler: /usr/bin/gcc -- Check for working C compiler: /usr/bin/gcc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Configuring done -- Generating done -- Build files have been written to: /opt/projects/ttiger/build brianli@sunrise:/opt/projects/ttiger/build$ make Scanning dependencies of target ttiger [ 16%] Building CXX object src/CMakeFiles/ttiger.dir/ttiger/exception/exception.o [ 33%] Building CXX object src/CMakeFiles/ttiger.dir/ttiger/util/test.o Linking CXX shared library ../lib/libttiger.so [ 33%] Built target ttiger Scanning dependencies of target ttiger_static [ 50%] Building CXX object src/CMakeFiles/ttiger_static.dir/ttiger/exception/exception.o [ 66%] Building CXX object src/CMakeFiles/ttiger_static.dir/ttiger/util/test.o Linking CXX static library ../lib/libttiger.a [ 66%] Built target ttiger_static Scanning dependencies of target mytest [ 83%] Building CXX object tests/CMakeFiles/mytest.dir/mytest.o Linking CXX executable ../bin/mytest [ 83%] Built target mytest Scanning dependencies of target ttiger_unittests [100%] Building CXX object unittests/CMakeFiles/ttiger_unittests.dir/ttiger_unittests.o Linking CXX executable ../bin/ttiger_unittests [100%] Built target ttiger_unittests brianli@sunrise:/opt/projects/ttiger/build$ ./bin/ttiger_unittests running main() from ttiger_unittests.cxx [==========] Running 1 test from 1 test case. [----------] Global test environment set-up. [----------] 1 test from Util [ RUN ] Util.Add_Test [ OK ] Util.Add_Test (0 ms) [----------] 1 test from Util (0 ms total) [----------] Global test environment tear-down [==========] 1 test from 1 test case ran. (1 ms total) [ PASSED ] 1 test. brianli@sunrise:/opt/projects/ttiger/build$