版权信息:可以任意转载, 转载时请务必以超链接形式标明文章原文出处 ,谢谢
原文出处: 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或者gtest
- build目录只是用来进行编译生成的,即进行out-of-source building
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$
分享到:
相关推荐
cmake 常用的环境变量,cmake 多文件多目录的学习的简易例子,cmake 学习的基础知识, cmake 常用的Linux代码目录构建方式
1、压缩包中打包文件为一个商用项目,由Makefile修改为cmake的源码,为了避免泄露源吗,源码中的头键词,经过批量替换,对敏感c码文件进行了删除。 2、cmake文件本身没有任问题,稍加修改,即可应用于自己项目。iMM_...
CMake添加引用多个子目录多个CMakeLists同时编译
cmake_makefile和Makefile文件cmake_makefile样式,导致自己的项目很难移植,也不方便在不同的其中中重用,但是却本人一开始喜欢在vs中开发调试代码,因此该项目可能非常适合开发初期且喜欢用vs调试的开发人员。...
CMake中文手册.pdf CMake中文手册.pdf CMake中文手册.pdf
工程内存在多个文件夹的cmake的编写,代码部分摘录字cisco的SIP协议栈
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性
本文是CMake官方文档CMake Tutorial (http://www.cmake.org/cmake/help/cmake_tutorial.html) 的翻译。通过一个样例工程从简单到复杂的完善过程,文档介绍了CMake主要模块(cmake, ctest, cpack)的功能和使用环境
使用cmake来在Android studio中调用三方so文件 使用cmake来在Android studio中调用三方so文件
在 linux 下使用 CMake 构建应用程序 级别:初级 王程明 (wangchengming.jlu@gmail.com), 硕士研究生, 吉林大学计算机科学与技术学院 2009 年 2 月 05 日 本文介绍了一个跨平台的自动化构建系统 CMake 在 ...
大型项目组件配置案例,参考Poco实现,如有不当之处请多多批评指正。环境ubuntu16.04,cmake版本3.5.1,其中test_cmake工程为案例,test_cmake_find用来依赖test_cmake工程。编译:1)在test_cmake/build下执行cmake ...
VS code配置步骤先按照网上的方法完成,大概就是安装mingw,cmake,VS code,然后安装VS code中的插件: 安装好之后,新建文件夹/项目如下: 其中,build是打开文件夹以后自动生成的。test中是源文件还有项目文件等...
1,背景知识:cmake 是 kitware 公司以及一些开源开发者在开发几个工具套件(VTK)的过程中衍生品,最终形成体系,成为一个独立的开放源代码项目。项目
CMake CMake附图 1 CMake 就是针对上面问题所设计的工具:它首先允许开发者编写一种平台无关的 CMakeList.txt 文件来定制...一些使用 CMake 作为项目架构系统的知名开源项目有 VTK、ITK、KDE、OpenCV、OSG 等 [1]。
Android Studio cmake方式jni开发,在c层使用其它动态链接库*.so
利用CMake来构建依赖glfw、glew、glm以及opengl核心库等库的OpenGL项目
一个简单的CMAKE例子,可以帮助大家理解使用cmake
使用vscode cmake插件构建的CMake工程模板
CMAKE中文教程
本资源搭建了一个cmake管理项目的框架。 1. 使用了.cmake模块文件封装功能 2. 使用cmake管理每个可执行程序的版本号 3. 提供脚本自动编译 直接运行.sh脚本即可完成编译。