CMake 是一种跨平台开源工具,用于定义在多个平台上运行的生成过程。
Visual Studio C 和 C++ 开发不再仅适用于 Windows。 本教程介绍如何在 Windows 和 Linux 上使用 Visual Studio C++ 进行跨平台开发。 由于它基于 CMake,无需创建或生成 Visual Studio 项目。 打开包含 CMakeLists.txt 文件的文件夹时,Visual Studio 会自动配置 IntelliSense 并生成设置。 可以快速开始在 Windows 上本地编辑、生成和调试代码。 然后,切换配置,在 Linux 上执行相同的操作,所有这些操作都从 Visual Studio 中进行。
针对常见的开源项目,遵循以下步骤,可以快速在本地IDE VS中生成可以调试的项目代码
1.CMakeLists.txt
包含在项目源码根目录的CMakeLists.txt与包含在单个项目文件夹中的CMakeLists.txt
2.CMakeSettings.json
vs对cmake的配置,可以方便的修改里面的属性字段达到预想的vs项目配置
单配置构建输出效果:
只有一个debug,无法切换
多配置构建输出效果:
同时包含Debug,MinSizeRel,Release,RelWithDebinfo四种支持的构建方式
"configurationType": "Debug;Release;RelWithDebInfo;MinSizeRel",
在configurationType字段设置四种支持的构建方式
如下编译kicad时的一个案例CMakeSettings.json
- {
- "environments": [
- {
- "BuildDir": "${workspaceRoot}\\build",
- "InstallDir": "${workspaceRoot}\\build\\install"
- },
- {
- "environment": "vcpkg",
- "VcPkgDir": "E:/vs2022/Projects/vcpkg"
- },
- {
- "environment": "mingw_64",
- "MINGW64_ROOT": "D:/msys64/mingw64",
- "BIN_ROOT": "${env.MINGW64_ROOT}/bin",
- "FLAVOR": "x86_64-w64-mingw32",
- "TOOLSET_VERSION": "10.2.0",
- "PATH": "${env.MINGW64_ROOT}/bin;${env.MINGW64_ROOT}/../usr/local/bin;${env.MINGW64_ROOT}/../usr/bin;${env.MINGW64_ROOT}/../bin;${env.PATH}",
- "INCLUDE": "${env.INCLUDE};${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION};${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/tr1;${env.MINGW64_ROOT}/include/c++/${env.TOOLSET_VERSION}/${env.FLAVOR}"
- }
- ],
- "configurations": [
- {
- "name": "x86-Debug",
- "generator": "Ninja",
- "configurationType": "Debug",
- "inheritEnvironments": [ "msvc_x86", "vcpkg" ],
- "buildRoot": "${env.BuildDir}\\${name}",
- "installRoot": "${env.InstallDir}\\${name}",
- "addressSanitizerEnabled": false,
- "cmakeCommandArgs": "",
- "buildCommandArgs": "-v",
- "ctestCommandArgs": "",
- "variables": [
- {
- "name": "KICAD_BUILD_QA_TESTS",
- "value": "False",
- "type": "BOOL"
- },
- {
- "name": "KICAD_WIN32_DPI_AWARE",
- "value": "ON",
- "type": "BOOL"
- }
- ],
- "cmakeToolchain": "${env.VcPkgDir}/scripts/buildsystems/vcpkg.cmake"
- },
- {
- "name": "x86-Release",
- "generator": "Ninja",
- "configurationType": "RelWithDebInfo",
- "inheritEnvironments": [ "msvc_x86", "vcpkg" ],
- "buildRoot": "${env.BuildDir}\\${name}",
- "installRoot": "${env.InstallDir}\\${name}",
- "cmakeCommandArgs": "",
- "buildCommandArgs": "-v",
- "ctestCommandArgs": "",
- "variables": [
- {
- "name": "KICAD_BUILD_QA_TESTS",
- "value": "False",
- "type": "BOOL"
- },
- {
- "name": "KICAD_WIN32_DPI_AWARE",
- "value": "ON",
- "type": "BOOL"
- }
- ],
- "cmakeToolchain": "${env.VcPkgDir}/scripts/buildsystems/vcpkg.cmake"
- },
- {
- "name": "x64-Debug",
- "generator": "Visual Studio 17 2022 Win64",
- "configurationType": "Debug;Release;RelWithDebInfo;MinSizeRel",
- "inheritEnvironments": [ "msvc_x64_x64", "vcpkg" ],
- "buildRoot": "${env.BuildDir}\\${name}",
- "installRoot": "${env.InstallDir}\\${name}",
- "cmakeCommandArgs": "-DSWIG_EXECUTABLE=E:/vs2022/Projects/vcpkg/swigwin-4.1.1/swig.exe",
- "buildCommandArgs": "-V",
- "ctestCommandArgs": "",
- "variables": [
- {
- "name": "KICAD_BUILD_QA_TESTS",
- "value": "False",
- "type": "BOOL"
- },
- {
- "name": "KICAD_WIN32_DPI_AWARE",
- "value": "ON",
- "type": "BOOL"
- }
- ],
- "cmakeToolchain": "${env.VcPkgDir}/scripts/buildsystems/vcpkg.cmake"
- },
- {
- "name": "x64-Release",
- "generator": "Visual Studio 17 2022 Win64",
- "configurationType": "RelWithDebInfo",
- "inheritEnvironments": [ "msvc_x64_x64", "vcpkg" ],
- "buildRoot": "${env.BuildDir}\\${name}",
- "installRoot": "${env.InstallDir}\\${name}",
- "cmakeCommandArgs": "-DSWIG_EXECUTABLE=E:/vs2022/Projects/vcpkg/swigwin-4.1.1/swig.exe",
- "buildCommandArgs": "-V",
- "ctestCommandArgs": "",
- "variables": [
- {
- "name": "KICAD_BUILD_QA_TESTS",
- "value": "False",
- "type": "BOOL"
- },
- {
- "name": "KICAD_WIN32_DPI_AWARE",
- "value": "ON",
- "type": "BOOL"
- }
- ],
- "cmakeToolchain": "${env.VcPkgDir}/scripts/buildsystems/vcpkg.cmake"
- },
- {
- "name": "MSYS2-x64-Debug",
- "generator": "Ninja",
- "configurationType": "Debug",
- "inheritEnvironments": [ "mingw_64" ],
- "buildRoot": "${env.BuildDir}\\${name}",
- "installRoot": "${env.InstallDir}\\${name}",
- "cmakeCommandArgs": "",
- "buildCommandArgs": "-v",
- "ctestCommandArgs": "",
- "intelliSenseMode": "linux-gcc-x64",
- "cmakeToolchain": "",
- "variables": [
- {
- "name": "CMAKE_C_COMPILER",
- "value": "${env.BIN_ROOT}/gcc.exe"
- },
- {
- "name": "CMAKE_CXX_COMPILER",
- "value": "${env.BIN_ROOT}/g++.exe"
- },
- {
- "name": "KICAD_BUILD_QA_TESTS",
- "value": "False",
- "type": "BOOL"
- },
- {
- "name": "KICAD_WIN32_DPI_AWARE",
- "value": "ON",
- "type": "BOOL"
- }
- ]
- }
- ]
- }