环境部署
即构建CMake项目之前的环境准备,请自行Google。涉及到各个平台(如linux)CMake、GCC、make、等等
Cmake
特性:跨平台、开源、提升编译效率、跨编译器版本(至少08之后、GCC 前向兼容cmake -G"Visual Studio 9 2008 Win64", 以上命令得用cd命令切换到顶层CMakeLists.txt的当前目录,才能生效)、支持单元测试(如GTEST
https://blog.youkuaiyun.com/sun_ashe/article/details/104527591/)
格式:CMakeLists.txt
功能: cmake这样的工具来帮助我们构建并维护项目代码。它能够输出各种各样的makefile或者project文件,简化编译构建过程和编译过程
Make
make工具可以看成是一个智能的批处理工具,它本身并没有编译和链接的功能,而是用类似于批处理的方式—通过调用makefile文件中用户指定的命令来进行编译和链接的。
跟make命令使用:

如果经常在Linux系统下面与C、C++文件打交道的话,一定经常遇到四/五步走:
mkdir build
cd build
cmake ..
make
// 如果是安装依赖库的时候会用到
make installhttps://zhuanlan.zhihu.com/p/102955723(安装到系统目录便于其他模块使用)
前两步并不难理解,就是想把对应项目的编译文件放在build目录下不用提交,这样便于管理。为什么要新建一个 build 文件夹?一般我们采用 cmake的out-of-source方式来构建(即生成的中间产物和源代码分离),这样做可以让生成的文件和源文件不会弄混,且目录结构看起来也会清晰明了。所以推荐使用这种方式,至于这个文件夹的命名并无限制,我们习惯命名为 build。
CMakeLists(支持大小写)
- 基础概念
- 环境变量的使用
- 预定义宏:
CMAKE_CURRENT_SOURCE_DIR cmake当前源文件根目录
PROJECT_SOURCE_DIR 当前项目的顶级源目录
CMAKE_COMPILER_IS_CUNCXX
- 基本语法
一个CMakeLists项目必须的也是最基本的几条:
project(xxx) #必须
add_subdirectory(子文件夹名称) #父目录必须,子目录不必
add_library(库文件名称 STATIC 文件) #通常子目录(二选一)
add_executable(可执行文件名称 文件) #通常父目录(二选一)
include_directories(路径) #必须
link_directories(路径) #必须
target_link_libraries(库文件名称/可执行文件名称 链接的库文件名称) #必须
- 项目实践
├── qaxtgs
│ ├── build
│ │ ├── CMakeCache.txt
│ │ ├── CMakeFiles
│ │ │ ├── 3.9.2
│ │ │ │ ├── CMakeCCompiler.cmake
│ │ │ │ ├── CMakeCXXCompiler.cmake
│ │ │ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ │ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ │ │ ├── CMakeSystem.cmake
│ │ │ │ ├── CompilerIdC
│ │ │ │ │ ├── a.out
│ │ │ │ │ ├── CMakeCCompilerId.c
│ │ │ │ │ └── tmp
│ │ │ │ └── CompilerIdCXX
│ │ │ │ ├── a.out
│ │ │ │ ├── CMakeCXXCompilerId.cpp
│ │ │ │ └── tmp
│ │ │ ├── cmake.check_cache
│ │ │ ├── CMakeDirectoryInformation.cmake
│ │ │ ├── CMakeOutput.log
│ │ │ ├── CMakeTmp
│ │ │ ├── feature_tests.bin
│ │ │ ├── feature_tests.c
│ │ │ ├── feature_tests.cxx
│ │ │ ├── Makefile2
│ │ │ ├── Makefile.cmake
│ │ │ ├── progress.marks
│ │ │ └── TargetDirectories.txt
│ │ ├── cmake_install.cmake
│ │ ├── common
│ │ │ ├── CMakeFiles
│ │ │ │ ├── CMakeDirectoryInformation.cmake
│ │ │ │ └── progress.marks
│ │ │ ├── cmake_install.cmake
│ │ │ ├── jsoncpp
│ │ │ │ ├── CMakeFiles
│ │ │ │ │ ├── CMakeDirectoryInformation.cmake
│ │ │ │ │ └── progress.marks
│ │ │ │ ├── cmake_install.cmake
│ │ │ │ └── Makefile
│ │ │ ├── Makefile
│ │ │ ├── platform
│ │ │ │ ├── CMakeFiles
│ │ │ │ │ ├── CMakeDirectoryInformation.cmake
│ │ │ │ │ └── progress.marks
│ │ │ │ ├── cmake_install.cmake
│ │ │ │ └── Makefile
│ │ │ └── QaxLog
│ │ │ ├── CMakeFiles
│ │ │ │ ├── CMakeDirectoryInformation.cmake
│ │ │ │ └── progress.marks
│ │ │ ├── cmake_install.cmake
│ │ │ └── Makefile
│ │ ├── Makefile
│ │ ├── qaxtgs
│ │ │ ├── CMakeFiles
│ │ │ │ ├── CMakeDirectoryInformation.cmake
│ │ │ │ ├── progress.marks
│ │ │ │ └── qaxtgs.dir
│ │ │ │ ├── build.make
│ │ │ │ ├── cmake_clean.cmake
│ │ │ │ ├── CXX.includecache
│ │ │ │ ├── DependInfo.cmake
│ │ │ │ ├── depend.internal
│ │ │ │ ├── depend.make
│ │ │ │ ├── dllmain.cpp.o
│ │ │ │ ├── fc_s_apis.cpp.o
│ │ │ │ ├── FileMonRule.cpp.o
│ │ │ │ ├── FileMonRuleMgr.cpp.o
│ │ │ │ ├── flags.make
│ │ │ │ ├── link.txt
│ │ │ │ ├── pch.cpp.o
│ │ │ │ ├── progress.make
│ │ │ │ ├── QaxTgsImpl.cpp.o
│ │ │ │ ├── ReportWorker.cpp.o
│ │ │ │ └── TgcDataConversion.cpp.o
│ │ │ ├── cmake_install.cmake
│ │ │ └── Makefile
│ │ └── README.md
│ ├── CMakeLists.txt
│ ├── common
│ │ ├── CMakeLists.txt
│ │ ├── interface
│ │ │ ├── fc_s_apis.h
│ │ │ ├── IQaxTgscBase.h
│ │ │ └── IQKFileMon.h
│ │ ├── jsoncpp
│ │ │ ├── CMakeLists.txt
│ │ │ ├── json
│ │ │ │ ├── allocator.h
│ │ │ │ ├── assertions.h
│ │ │ │ ├── autolink.h
│ │ │ │ ├── config.h
│ │ │ │ ├── features.h
│ │ │ │ ├── forwards.h
│ │ │ │ ├── json.h
│ │ │ │ ├── reader.h
│ │ │ │ ├── value.h
│ │ │ │ ├── version.h
│ │ │ │ └── writer.h
│ │ │ ├── JsonHelper.h
│ │ │ └── src
│ │ │ ├── json_reader.cpp
│ │ │ ├── json_tool.h
│ │ │ ├── json_value.cpp
│ │ │ ├── json_valueiterator.inl
│ │ │ └── json_writer.cpp
│ │ ├── platform
│ │ │ ├── api_os_util.h
│ │ │ ├── CMakeLists.txt
│ │ │ ├── dlfcn.h
│ │ │ ├── DllLoader.h
│ │ │ ├── platform_Thread.cpp
│ │ │ ├── platform_Thread.h
│ │ │ ├── str.h
│ │ │ ├── StringUtils.cpp
│ │ │ ├── StringUtils.h
│ │ │ └── XdFileSystem.h
│ │ └── QaxLog
│ │ ├── CMakeLists.txt
│ │ ├── QaxLog.cpp
│ │ ├── QaxLog.h
│ │ └── Qaxlog.ini
│ └── qaxtgs
│ ├── bin
│ │ └── x64
│ │ └── Release
│ │ └── libqaxtgs.so
│ ├── CMakeLists.txt
│ ├── dllmain.cpp
│ ├── fc_s_apis.cpp
│ ├── FileMonRule.cpp
│ ├── FileMonRule.h
│ ├── FileMonRuleMgr.cpp
│ ├── FileMonRuleMgr.h
│ ├── framework.h
│ ├── lib
│ │ └── libqaxtgs.so
│ ├── obj
│ │ └── x64
│ │ └── Release
│ │ ├── dllmain.o
│ │ ├── fc_s_apis.o
│ │ ├── FileMonRuleMgr.o
│ │ ├── FileMonRule.o
│ │ ├── json_reader.o
│ │ ├── json_value.o
│ │ ├── json_writer.o
│ │ ├── pch.o
│ │ ├── platform_Thread.o
│ │ ├── QaxLog.o
│ │ ├── QaxTgsImpl.o
│ │ ├── ReportWorker.o
│ │ ├── StringUtils.o
│ │ └── TgcDataConversion.o
│ ├── pch.cpp
│ ├── pch.h
│ ├── QaxTgsImpl.cpp
│ ├── QaxTgsImpl.h
│ ├── ReportWorker.cpp
│ ├── ReportWorker.h
│ ├── TgcDataConversion.cpp
│ ├── TgcDataConversion.h
│ └── TgcPolicyServer.json
└── test
├── bin
│ └── x64
│ └── Release
│ └── test.out
├── CMakeLists.txt
├── include
│ └── dlfcn.h
├── obj
│ └── x64
│ └── Release
│ └── main.o
└── src
└── main.cpp
步骤(顺序无所谓、搞清楚原理GUI->API):
- 根目录CMakeLists


- 各子目录CMakeLists

解释:
Q1:CMakeLists应该出现在工程的那些位置,那些不需要
A1:CMakeLists粒度是二进制文件(exe\so\dll\lib\a\out\...)
QA
Q: 为什么选cmake,vs不就可以?
A: 刚接触的时候深深体会到cmake的方便:如果目标平台是windows,它可以帮你自动构建出vs工程;如果是安卓,自动构建出eclipse工程,如果是IOS,自动构建出xcode工程。想想以前用vs建工程的时候,如果要引入第三方库,必须要手动配置第三方库路径,如果引入的库少,那还没什么,如果多的话就悲剧了,配个环境都要半天。再想想以前在linux平台下手动写Makefile的时候,如果工程比较小,模块少还好办,如果工程大,模块多,各种写依赖关系都要让你抓狂。有了cmake这个工具,我们完全可以靠它来帮我们构建vs工程,写Makefile文件。既然cmake构建工程这么方便,当然需要拿来用
Q: 原理书推荐?训练git?
A: 《程序员的自我修养—链接、装载与库》& https://github.com/ttroy50/cmake-examples
2835



被折叠的 条评论
为什么被折叠?



