首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

CMake在多级目录项目中的容易使用

2012-07-01 
CMake在多级目录项目中的简单使用版权信息:可以任意转载, 转载时请务必以超链接形式标明文章原文出处 ,谢

CMake在多级目录项目中的简单使用

版权信息:可以任意转载, 转载时请务必以超链接形式标明文章原文出处 ,谢谢
原文出处: 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库为主体的项目。

    在ttiger库目录中,存在有include和src目录。其分别是记录下该ttiger项目的头文件和源文件。为了便于项目组织,所以我将每一个功能模块放在一个单独的目录下,用单独的namespace来表示,有点类似于Java的组织方式,如在图中存在有 ttiger::exception::Exception这个类(src/ttiger/exception/exception.cxx)在tests中存在一些使用了该ttiger库的程序,在上面里面只是写了一个简单的mytest.cxx文件,其使用了ttiger库里面一部分功能在unittests中存在的是对ttiger进行单元测试的测试用例。使用了google的test框架 google-test或者gtestbuild目录只是用来进行编译生成的,即进行out-of-source building

CMakeLists示例

上面简单地介绍了项目的布局,那么下面就记录下项目中应用到的CMakeList.txt

根目录

因为CMakeLists.txt是嵌套的,下面先介绍最上面的

?

# the whole CMakeLists.txt for project ttigerPROJECT(ttiger)CMAKE_MINIMUM_REQUIRED(VERSION 2.4)# include directoriesINCLUDE_DIRECTORIES(  ${PROJECT_SOURCE_DIR}/include  ${PROJECT_SOURCE_DIR}/include/ttiger  /usr/local/include  /usr/include)# lib directoriesLINK_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 directoryPROJECT(ttiger)SET(TTIGER_SRCS  ttiger/exception/exception.cxx  ttiger/util/test.cxx)# shared libraryADD_LIBRARY(ttiger SHARED ${TTIGER_SRCS})# static libraryADD_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.txtADD_EXECUTABLE(mytest mytest.cxx)TARGET_LINK_LIBRARIES(mytest ttiger)

其就是用ADD_EXECUTABLE来告诉编译啥可执行文件,用TARGET_LINK_LIBRARIES来指定使用了那些库,这样就可以编译出来mytest可执行文件了

单元测试目录

# CMakeLists.txt for directory unittestsADD_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/buildbrianli@sunrise:/opt/projects/ttiger/build$ makeScanning 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.oLinking CXX shared library ../lib/libttiger.so[ 33%] Built target ttigerScanning 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.oLinking CXX static library ../lib/libttiger.a[ 66%] Built target ttiger_staticScanning dependencies of target mytest[ 83%] Building CXX object tests/CMakeFiles/mytest.dir/mytest.oLinking CXX executable ../bin/mytest[ 83%] Built target mytestScanning dependencies of target ttiger_unittests[100%] Building CXX object unittests/CMakeFiles/ttiger_unittests.dir/ttiger_unittests.oLinking CXX executable ../bin/ttiger_unittests[100%] Built target ttiger_unittestsbrianli@sunrise:/opt/projects/ttiger/build$ ./bin/ttiger_unittestsrunning 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$

?

热点排行