CMake在多级⽬录项⽬中的简单使⽤
引⾔
在项⽬⽐较⼩的时候,只需要简单地编写Makefile⽂件就可以完成了对项⽬的管理。随着项⽬的规模⽇益增⼤,使⽤⼀个好的构建⼯具来管理项⽬, 变得⾮常重要。过去⼀般是使⽤autoconf, automake那⼀套来解决,即./configure, make, make install。由于⾃⼰对autoconf不是很懂也不感冒,所以没有怎么去⽤它。最近在看到了CMake,这是⼀个夸平台的项⽬构建⼯具,很简单,夸 平台,对我来说,能够简单地上⼿并且解决所需要的需求。所以写下这个CMake简单使⽤的博客,记录下来⽅便将来查阅。
项⽬简介
⾸先给出项⽬的⼀个布局(layout)吧。
brianli@sunrise:/opt/projects/ttiger$ tree
.
|--
|-- build
|-- include
|  `-- ttiger
|      |-- exception
|      |  `-- exception.hpp
|      `-- util
|          `-- test.hpp
|-- src
|  |--
|  `-- ttiger
|      |-- exception
|      |  `--
|      `-- util
|          `--
|-- tests
|  |--
|  `--
`-- unittests
|--
`--
可以看到,在这个项⽬是⼀个以ttiger库为主体的项⽬。
1. 在ttiger库⽬录中,存在有include和src⽬录。其分别是记录下该ttiger项⽬的头⽂件和源⽂件。为了便于项⽬组织,所以我将每 ⼀个
功能模块放在⼀个单独的⽬录下,⽤单独的namespace来表⽰,有点类似于Java的组织⽅式,如在图中存在有
ttiger::exception::Exception这个类(src/ttiger/)
2. 在tests中存在⼀些使⽤了该ttiger库的程序,在上⾯⾥⾯只是写了⼀个简单的⽂件,其使⽤了ttiger库⾥⾯⼀部 分功能
3. 在unittests中存在的是对ttiger进⾏单元测试的测试⽤例。使⽤了google的test框架 google-test或者gtest
4. build⽬录只是⽤来进⾏编译⽣成的,即进⾏out-of-source building
CMakeLists⽰例
上⾯简单地介绍了项⽬的布局,那么下⾯就记录下项⽬中应⽤到的
根⽬录
因为是嵌套的,下⾯先介绍最上⾯的
# the 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 for src directory
PROJECT(ttiger)
SET(TTIGER_SRCS
ttiger/
ttiger/
)
# 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库的应⽤程序的⽂件
# test
ADD_EXECUTABLE()
TARGET_LINK_LIBRARIES(mytest ttiger)
其就是⽤ADD_EXECUTABLE来告诉编译啥可执⾏⽂件,⽤TARGET_LINK_LIBRARIES来指定使⽤了那些库,这样就可以编译 出来mytest可执⾏⽂件了
单元测试⽬录
# for directory unittests
ADD_EXECUTABLE(ttiger_unittests )
TARGET_LINK_LIBRARIES(ttiger_unittests ttiger;gtest;pthread
具体情况跟应⽤程序⽬录⼀样
为什么现在都用cmake
应⽤程序编译与执⾏
所有的都已经写好了,开始编译吧
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
[==========] 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$

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。