网站上线之后要做的工作推广公众号的9种方法
一、CMake简介
CMake是一个跨平台的构建工具,它可以生成各种操作系统和编译器的构建文件。CMake使用一种基于文本(CMakeLists.txt)的语言来描述项目及其依赖关系,并生成相应的构建文件。
CMake的基本概念包括:
- 变量:用于存储各种信息,例如编译器选项、路径等。
- 函数:用于执行各种操作,例如检查系统版本、生成Makefile等。
- 命令:用于执行各种任务,例如添加源文件、设置属性等。
- 包:用于组织和管理CMake代码。
CMake的使用非常灵活,可以根据不同的需求进行定制。它支持多种构建系统,包括Makefile、Ninja、Visual Studio等。同时,CMake也提供了丰富的API和脚本语言,方便用户进行二次开发和集成。
CMake - Upgrade Your Software Build System
Getting Started with CMake
CMake Reference Documentation — CMake 3.28.0-rc6 Documentation
二、安装CMake
Windows
在Windows下安装CMake,可以按照以下步骤进行:
1. 下载CMake安装包
访问CMake官方网站 https://cmake.org/download/
下载适用于Windows的CMake安装包。选择适合你的系统版本(32位或64位)的安装包进行下载。
2. 运行安装程序
双击下载好的CMake安装包,启动安装程序。
3. 选择安装路径
在安装向导中,选择CMake的安装路径。建议将CMake安装在C:\Program Files\CMake
目录下,以便于在命令行中直接使用。
4. 完成安装
点击“Install”按钮,开始安装CMake。安装完成后,点击“Finish”按钮退出安装向导。
5. 验证安装
打开命令提示符(按Win+R键,输入cmd
,回车),输入cmake --version,
按回车键。如果显示出CMake的版本信息,说明安装成功。
至此,CMake已经成功安装在你的Windows系统中。你可以开始使用CMake进行跨平台项目构建了。
三、CMake-GUI工具
我们一般都是通过“cmake命令行” 来运行CMake,CMake-GUI作为CMake的一个图形化界面工具,可以帮助开发者更方便地创建和管理CMake项目。通过使用CMake-GUI,开发者可以提高开发效率,实现跨平台项目的快速构建。
可以通过windows “cmake-gui”菜单启动:
四、CMake 命令行
cmake --help
命令用于查看CMake的帮助信息,包括各种参数的使用说明和场景。通过使用--help
参数,用户可以快速了解CMake的基本用法和常用命令。
通过cmake命令行,可以做所有CMake-GUI能做的事情,并且更方便通过脚本来构建CMake项目。
C:\Users\Laolang>cmake --help
Usagecmake [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 bygenerator.-A <platform-name> = Specify platform name if supported bygenerator.--toolchain <file> = Specify toolchain file[CMAKE_TOOLCHAIN_FILE].--install-prefix <directory> = Specify install directory[CMAKE_INSTALL_PREFIX].-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 warningserrors.-Wno-error=deprecated = Make deprecated macro and function warningsnot errors.--preset <preset>,--preset=<preset>= Specify a configure preset.--list-presets[=<type>] = List available presets.-E = CMake command mode.-L[A][H] = List non-advanced cached variables.--fresh = Configure a fresh build tree, removing anyexisting cache file.--build <dir> = Build a CMake-generated project binary tree.--install <dir> = Install a CMake-generated project binarytree.--open <dir> = Open generated project in the associatedapplication.-N = View mode only.-P <file> = Process script mode.--find-package = Legacy pkg-config like mode. Do not use.--graphviz=<file> = Generate graphviz of dependencies, seeCMakeGraphVizOptions.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 CMakefiles. --loglevel is also accepted forbackward 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.--debug-find-pkg=<pkg-name>[,...]= Limit cmake debug-find to thecomma-separated list of packages--debug-find-var=<var-name>[,...]= Limit cmake debug-find to thecomma-separated list of result variables--trace = Put cmake in trace mode.--trace-expand = Put cmake in trace mode with variableexpansion.--trace-format=<human|json-v1>= Set the output format of the trace.--trace-source=<file> = Trace only this CMake file/module. Multipleoptions allowed.--trace-redirect=<file> = Redirect trace output to a file instead ofstderr.--warn-uninitialized = Warn about uninitialized values.--no-warn-unused-cli = Don't warn about command line options.--check-system-vars = Find problems with variable usage in systemfiles.--compile-no-warning-as-error= Ignore COMPILE_WARNING_AS_ERROR property andCMAKE_COMPILE_WARNING_AS_ERROR variable.--profiling-format=<fmt> = Output data for profiling CMake scripts.Supported formats: google-trace--profiling-output=<file> = Select an output path for the profiling dataenabled through --profiling-format.-h,-H,--help,-help,-usage,/? = Print usage information and exit.--version,-version,/V [<file>]= Print version number and exit.--help <keyword> [<file>] = Print help for one keyword and exit.--help-full [<file>] = Print all help manuals and exit.--help-manual <man> [<file>] = Print one help manual and exit.--help-manual-list [<file>] = List help manuals available and exit.--help-command <cmd> [<file>]= Print help for one command and exit.--help-command-list [<file>] = List commands with help available and exit.--help-commands [<file>] = Print cmake-commands manual and exit.--help-module <mod> [<file>] = Print help for one module and exit.--help-module-list [<file>] = List modules with help available and exit.--help-modules [<file>] = Print cmake-modules manual and exit.--help-policy <cmp> [<file>] = Print help for one policy and exit.--help-policy-list [<file>] = List policies with help available and exit.--help-policies [<file>] = Print cmake-policies manual and exit.--help-property <prop> [<file>]= Print help for one property and exit.--help-property-list [<file>]= List properties with help available andexit.--help-properties [<file>] = Print cmake-properties manual and exit.--help-variable var [<file>] = Print help for one variable and exit.--help-variable-list [<file>]= List variables with help available and exit.--help-variables [<file>] = Print cmake-variables manual and exit.GeneratorsThe following generators are available on this platform (* marks default):
* Visual Studio 17 2022 = Generates Visual Studio 2022 project files.Use -A option to specify architecture.Visual Studio 16 2019 = Generates Visual Studio 2019 project files.Use -A option to specify architecture.Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.Optional [arch] can be "Win64" or "ARM".Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files.Optional [arch] can be "Win64" or "ARM".Visual Studio 12 2013 [arch] = Deprecated. Generates Visual Studio 2013project files. Optional [arch] can be"Win64" or "ARM".Visual Studio 9 2008 [arch] = Deprecated. Generates Visual Studio 2008project files. Optional [arch] can be"Win64" or "IA64".Borland Makefiles = Generates Borland makefiles.NMake Makefiles = Generates NMake makefiles.NMake Makefiles JOM = Generates JOM makefiles.MSYS Makefiles = Generates MSYS makefiles.MinGW Makefiles = Generates a make file for use withmingw32-make.Green Hills MULTI = Generates Green Hills MULTI files(experimental, work-in-progress).Unix Makefiles = Generates standard UNIX makefiles.Ninja = Generates build.ninja files.Ninja Multi-Config = Generates build-<Config>.ninja files.Watcom WMake = Generates Watcom WMake makefiles.CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files(deprecated).CodeBlocks - NMake Makefiles = Generates CodeBlocks project files(deprecated).CodeBlocks - NMake Makefiles JOM= Generates CodeBlocks project files(deprecated).CodeBlocks - Ninja = Generates CodeBlocks project files(deprecated).CodeBlocks - Unix Makefiles = Generates CodeBlocks project files(deprecated).CodeLite - MinGW Makefiles = Generates CodeLite project files(deprecated).CodeLite - NMake Makefiles = Generates CodeLite project files(deprecated).CodeLite - Ninja = Generates CodeLite project files(deprecated).CodeLite - Unix Makefiles = Generates CodeLite project files(deprecated).Eclipse CDT4 - NMake Makefiles= Generates Eclipse CDT 4.0 project files(deprecated).Eclipse CDT4 - MinGW Makefiles= Generates Eclipse CDT 4.0 project files(deprecated).Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files(deprecated).Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files(deprecated).Kate - MinGW Makefiles = Generates Kate project files (deprecated).Kate - NMake Makefiles = Generates Kate project files (deprecated).Kate - Ninja = Generates Kate project files (deprecated).Kate - Ninja Multi-Config = Generates Kate project files (deprecated).Kate - Unix Makefiles = Generates Kate project files (deprecated).Sublime Text 2 - MinGW Makefiles= Generates Sublime Text 2 project files(deprecated).Sublime Text 2 - NMake Makefiles= Generates Sublime Text 2 project files(deprecated).Sublime Text 2 - Ninja = Generates Sublime Text 2 project files(deprecated).Sublime Text 2 - Unix Makefiles= Generates Sublime Text 2 project files(deprecated).
常用命令行参数
-G参数
-G
是CMake命令行中的一个参数,用于指定生成器。
CMake Generator是CMake构建系统中的一个概念,它指定了用于生成构建文件的工具。在CMake中,Generator是一个字符串,它告诉CMake使用哪个工具来生成构建文件(如Makefile或Visual Studio解决方案)。
CMake支持多种生成器,每种生成器都有自己的特点和用途。例如,Ninja生成器可以提供更快的构建速度,而Unix Makefiles生成器则适用于Unix/Linux系统。
使用案例:
cmake -G "Visual Studio 16 2019" path/to/source
这个例子中,我们使用-G
参数指定了生成器为Visual Studio 16 2019。然后,我们将源代码所在的路径传递给CMake命令。这样,CMake会使用Visual Studio 16 2019作为生成器来构建项目。
-D参数
-D
是CMake命令行中的一个参数,用于定义变量。在CMake中,变量可以用于存储各种信息,例如编译器选项、路径等。使用-D
参数可以在命令行中定义这些变量。
使用案例:
cmake -D CMAKE_BUILD_TYPE=Debug path/to/source
这个例子中,我们使用-D
参数定义了一个名为CMAKE_BUILD_TYPE
的变量,并将其值设置为Debug
。然后,我们将源代码所在的路径传递给CMake命令。这样,CMake会使用Debug模式来构建项目。
除了CMAKE_BUILD_TYPE
,CMake还支持许多其他变量,例如CMAKE_INSTALL_PREFIX
、CMAKE_CXX_STANDARD
等。使用-D
参数可以在命令行中定义这些变量,从而方便地控制CMake的构建过程。
-B参数
-B
是CMake命令行中的一个参数,用于指定构建目录。在CMake中,构建目录是用来存放生成的构建文件(如Makefile或Visual Studio解决方案)的目录。
使用案例:
cmake -B build path/to/source
这个例子中,我们使用-B
参数指定了一个名为build
的构建目录。然后,我们将源代码所在的路径传递给CMake命令。这样,CMake会在build
目录下生成构建文件。
默认情况下,CMake会在当前目录下生成构建文件。但是,有时候我们需要将构建文件放在其他目录下,这时就可以使用-B
参数来指定构建目录。
-E参数
-E
是CMake命令行中的一个参数,用于执行CMake脚本中的命令。在CMake中,-E
参数可以用来执行各种命令,例如生成变量、运行脚本等。
使用案例:
cmake -E echo "Hello, World!"
这个例子中,我们使用-E
参数执行了一个简单的echo
命令,输出了字符串Hello, World!
。
cmake -E make_directory build
这个例子中,这将在当前目录下创建一个名为build的目录。
cmake -E copy test.txt build/test.txt
这个例子中,这将将test.txt文件从当前目录复制到build目录中。
除了echo
,CMake还支持许多其他命令,例如if
、foreach
、list
等。使用-E
参数可以在命令行中执行这些命令,从而方便地控制CMake的构建过程。
--build参数
--build
是CMake命令行中的一个参数,用于指定构建目录。在CMake中,构建目录是用来存放生成的构建文件(如Makefile或Visual Studio解决方案)的目录。
使用案例:
cmake --build path/to/build
这个例子中,我们使用--build
参数指定了一个名为path/to/build
的构建目录。然后,CMake会在该目录下执行构建过程。
默认情况下,CMake会在当前目录下生成构建文件,并在当前目录下执行构建过程。但是,有时候我们需要将构建文件放在其他目录下,或者在其他目录下执行构建过程,这时就可以使用--build
参数来指定构建目录。
--install参数
--install
是CMake命令行中的一个参数,用于指定安装目录。在CMake中,安装目录是用来存放生成的安装文件(如Windows下的msi文件或Linux下的tar.gz文件)的目录。
使用案例:
cmake --install path/to/build --prefix=path/to/install
这个例子中,我们使用--install
参数指定了一个名为path/to/build
的构建目录,并使用--prefix
参数指定了一个名为path/to/install
的安装目录。然后,CMake会在该安装目录下执行安装过程。
默认情况下,CMake会在当前目录下生成安装文件,并在当前目录下执行安装过程。但是,有时候我们需要将安装文件放在其他目录下,或者在其他目录下执行安装过程,这时就可以使用--install
参数来指定安装目录。
--graphviz参数
--graphviz
是CMake命令行中的一个参数,用于生成GraphViz图形。在CMake中,GraphViz是一种常用的可视化工具,可以用来绘制构建文件的依赖关系图等。
使用案例:
cmake --graphviz path/to/output.dot
这个例子中,我们使用--graphviz
参数指定了一个名为path/to/output.dot
的输出文件。然后,CMake会在该文件中生成GraphViz图形。
默认情况下,CMake不会生成GraphViz图形。但是,有时候我们需要查看构建文件的依赖关系等信息,这时就可以使用--graphviz
参数来生成GraphViz图形。
--help-variable-list参数
--help-variable-list
是CMake命令行中的一个参数,用于列出所有可用的变量。在CMake中,变量可以用于存储各种信息,例如编译器选项、路径等。使用--help-variable-list
参数可以查看所有可用的变量及其描述。
使用案例:
cmake --help-variable-list
这个例子中,我们使用--help-variable-list
参数来列出所有可用的变量及其描述。CMake会输出一个帮助信息,其中包含所有可用的变量及其描述。
默认情况下,CMake不会输出帮助信息。但是,有时候我们需要查看所有可用的变量及其描述,这时就可以使用--help-variable-list
参数来列出所有可用的变量及其描述。
--help-variable参数
--help-variable
是CMake命令行中的一个参数,用于显示指定变量的值。在CMake中,变量可以用于存储各种信息,例如编译器选项、路径等。使用--help-variable
参数可以查看指定变量的值。
使用案例:
cmake --help-variable VARIABLE_NAME
这个例子中,我们使用--help-variable
参数来显示名为VARIABLE_NAME
的变量的值。CMake会输出该变量的值及其描述。
默认情况下,CMake不会输出帮助信息。但是,有时候我们需要查看指定变量的值及其描述,这时就可以使用--help-variable
参数来显示指定变量的值及其描述。
--help-variables参数
--help-variables
是CMake命令行中的一个参数,用于列出所有可用的变量及其描述。在CMake中,变量可以用于存储各种信息,例如编译器选项、路径等。使用--help-variables
参数可以查看所有可用的变量及其描述。
使用案例:
cmake --help-variables
这个例子中,我们使用--help-variables
参数来列出所有可用的变量及其描述。CMake会输出一个帮助信息,其中包含所有可用的变量及其描述。
cmake --help-variables cmake_variables.txt
这个例子中,我们使用--help-variables
参数来输出所有可用的变量及其描述到当前目录的cmake_variables.txt,方便查看。
默认情况下,CMake不会输出帮助信息。但是,有时候我们需要查看所有可用的变量及其描述,这时就可以使用--help-variables
参数来列出所有可用的变量及其描述。
--help-property、 --help-property-list、--help-properties参数
--help-property
是CMake命令行中的一个参数,用于显示指定属性的帮助信息。在CMake中,属性可以用于存储各种信息,例如编译器选项、路径等。使用--help-property
参数可以查看指定属性的帮助信息。
使用案例:
cmake --help-property VARIABLE_NAME [<file>]
这个例子中,我们使用--help-property
参数来显示名为VARIABLE_NAME
的属性的帮助信息。CMake会输出该属性的描述及其可用选项。如果指定了文件名,则将帮助信息输出到该文件中。
除了--help-property
,CMake还提供了其他两个与属性相关的参数:--help-property-list
和--help-properties
。
--help-property-list
参数用于列出所有具有可用帮助信息的属性,并退出。
--help-properties
参数用于打印cmake-properties手册并退出。
这些参数可以帮助用户更好地了解CMake的属性及其用法。
-- help-command、--help-command-list、--help-commands参数
--help-command
是CMake命令行中的一个参数,用于显示指定命令的帮助信息。在CMake中,有许多命令可以用于构建和配置项目,例如add_executable
、configure
等。使用--help-command
参数可以查看指定命令的帮助信息。
使用案例:
cmake --help-command <command> [<file>]
这个例子中,我们使用--help-command
参数来显示名为<command>
的命令的帮助信息。CMake会输出该命令的描述及其可用选项。如果指定了文件名,则将帮助信息输出到该文件中。
除了--help-command
,CMake还提供了其他两个与命令相关的参数:--help-list-commands
和--help-all-commands
。
--help-list-commands
参数用于列出所有可用的命令及其简要描述,并退出。
--help-all-commands
参数用于打印所有可用命令的手册页并退出。
这些参数可以帮助用户更好地了解CMake的命令及其用法。
常用案例
# 指定构建类型为Release:
cmake -DCMAKE_BUILD_TYPE=Release /path/to/source#指定安装目录为/usr/local:
cmake -DCMAKE_INSTALL_PREFIX=/usr/local /path/to/source#指定使用Android NDK作为交叉编译工具链:
cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/android.toolchain.cmake /path/to/source#指定使用C++11标准:
cmake -DCMAKE_CXX_FLAGS=-std=c++11 /path/to/source#定义CMake变量:
cmake -DVAR=value /path/to/source#调用CMake内置命令echo:
cmake -E echo "Hello World"#指定构建目录:
cmake -B /path/to/build /path/to/source#指定使用gcc编译器:
cmake -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ /path/to/source#指定构建共享库:
cmake -DBUILD_SHARED_LIBS=ON /path/to/source#指定生成的可执行文件和库文件的输出目录:
cmake -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=/path/to/bin -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/path/to/lib /path/to/source指定使用Ninja生成器:
cmake -G Ninja /path/to/source指定CMake模块路径:
cmake -DCMAKE_MODULE_PATH=/path/to/modules /path/to/source指定导出编译命令:
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON /path/to/source指定运行时动态链接库搜索路径:
cmake -DCMAKE_INSTALL_RPATH=/path/to/lib /path/to/source指定使用Clang编译器且开启优化选项:
cmake -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_FLAGS=-O2 -DCMAKE_CXX_FLAGS=-O2 /path/to/source指定使用Debug构建类型:
cmake -DCMAKE_BUILD_TYPE=Debug /path/to/source指定使用Release构建类型,并生成Makefile文件:
cmake -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" /path/to/source指定第三方库安装路径:
cmake -DCMAKE_PREFIX_PATH=/path/to/third_party /path/to/source指定使用GCC编译器且开启调试信息:
cmake -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_FLAGS=-g -DCMAKE_CXX_FLAGS=-g /path/to/source
五、总结
通过学习和掌握cmake --help
命令行参数,我们可以更好地利用CMake进行跨平台项目构建,提高开发效率。在实际项目中,我们可以根据需要选择合适的参数来满足不同的构建需求。