---cmake(1)--(3)__cmake(3):编译库和链接可执行文件
cmake(1):cmake简介及安装
1. CMake简介
CMake是一个用于管理源代码的跨平台构建工具,可以方便地根据目标平台和编译工具产生对应的编译文件,如基于Linux系统生成对应的Makefile文件或Widows Virtual Studio生成project等。虽然最主要用于C/C++语言的构建,但是也可以用于其它编程语言的源代码。
如同使用make命令工具解析Makefile文件一样,cmake命令工具依赖于一个CMakeLists.txt的文件,该文件定义了代码的编译规则和目标等信息。
官方文档说明:https://cmake.org/cmake/help/v3.17/index.html
2. 安装CMake
下面以Ubuntu 16.04安装cmake为例。
2.1 使用命令在线安装
sudo apt-get install cmake
这种方式需要联网且安装的是链接库中的版本,可能不是最新或指定的cmake版本。
如果能联网且希望安装指定版本可以使用如下简单方法:
从官网上下载对应版本的.sh文件,如:cmake-3.17.2-Linux-x86_64.sh
运行该文件:
./cmake-3.17.2-Linux-x86_64.sh
根据提示输入对应命令即可完成安装。
2.2 使用源代码编译安装
1. 下载源代码
从官网:https://cmake.org/download中下载适合的版本。
我这里下载的3.17.2版本
2. 编译
#解压
tar xvf cmake-3.17.2.tar.gz
cd cmake-3.17.2
./boostrap
make -j32
3. 安装
sudo make install
4. 确认是否安装成功
cmake --version
显示如下:
注:不同平台可能会遇到依赖问题,相应解决即可。
3. CMake基本命令
# cmake -h
Usage
cmake [options] <path-to-source>
cmake [options] <path-to-existing-build>
cmake [options] -S <path-to-source> -B <path-to-build>
Specify a source directory to (re-)generate a build system for it in the
current working directory. Specify an existing build directory to
re-generate its build system.
Options
-S <path-to-source> = Explicitly specify a source directory.
-B <path-to-build> = Explicitly specify a build directory.
-C <initial-cache> = Pre-load a script to populate the cache.
-D <var>[:<type>]=<value> = Create or update a cmake cache entry.
-U <globbing_expr> = Remove matching entries from CMake cache.
-G <generator-name> = Specify a build system generator.
-T <toolset-name> = Specify toolset name if supported by
generator.
-A <platform-name> = Specify platform name if supported by
generator.
-Wdev = Enable developer warnings.
-Wno-dev = Suppress developer warnings.
-Werror=dev = Make developer warnings errors.
-Wno-error=dev = Make developer warnings not errors.
-Wdeprecated = Enable deprecation warnings.
-Wno-deprecated = Suppress deprecation warnings.
-Werror=deprecated = Make deprecated macro and function warnings
errors.
-Wno-error=deprecated = Make deprecated macro and function warnings
not errors.
-E = CMake command mode.
-L[A][H] = List non-advanced cached variables.
--build <dir> = Build a CMake-generated project binary tree.
--install <dir> = Install a CMake-generated project binary
tree.
--open <dir> = Open generated project in the associated
application.
-N = View mode only.
-P <file> = Process script mode.
--find-package = Run in pkg-config like mode.
--graphviz=[file] = Generate graphviz of dependencies, see
CMakeGraphVizOptions.cmake for more.
--system-information [file] = Dump information about this system.
--log-level=<ERROR|WARNING|NOTICE|STATUS|VERBOSE|DEBUG|TRACE>
= Set the verbosity of messages from CMake
files. --loglevel is also accepted for
backward compatibility reasons.
--log-context = Prepend log messages with context, if given
--debug-trycompile = Do not delete the try_compile build tree.
Only useful on one try_compile at a time.
--debug-output = Put cmake in a debug mode.
--debug-find = Put cmake find in a debug mode
