繁体   English   中英

基于CMAKE的Qt Creator项目,如何指向VCPKG?

[英]On Qt Creator project based on CMAKE, how to point to VCPKG?

在基于 QMAKE 的 (macOS silicon) Qt Creator 项目上,我必须添加以下内容才能找到 VCPKG:

QMAKE_APPLE_DEVICE_ARCHS = arm64

CONFIG(debug, debug|release) {
     MODE = debug
     d = d
} else {

}

INCLUDEPATH += $$(VCPKG)/include
     LIBS += -L$$(VCPKG)/$${MODE}/lib
     LIBS += -lfmt$${d}

考虑export VCPKG=~/vcpkg/installed/arm64-osx

CMAKE呢? 我基于它创建了一个 C++ 项目,如下所示:

cmake_minimum_required(VERSION 3.5)

project(untitled1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(untitled1 main.cpp)

install(TARGETS untitled1
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

vcpkg 说明用于In order to use vcpkg with CMake outside of an IDE

cmake -B [build directory] -S . -DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake

如何在 IDE 中使用 vcpkg 和 CMake?

正如说明所说,您根本不应该更改您的 CMake 文件并在命令行中传递它:

cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake

这假设环境变量VCPKG_ROOT被定义为指向 vcpkg 安装。

您可以在CMakePresets.json文件中添加工具链命令行参数。

然后,为了确保 vcpkg 安装您需要的东西,我还会创建一个清单文件:

{
    "name": "my-project",
    "version-string": "0.1.0",
    "dependencies": [
        "fmt"
        // or any other dependencies
    ]
}

然后,您的 CMake 文件应该可以简单地找到这些包:

cmake_minimum_required(VERSION 3.5)

project(untitled1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(untitled1 main.cpp)

install(TARGETS untitled1
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

find_package(fmt REQUIRED)

target_link_libraries(untitled1 PUBLIC fmt::fmt)

vcpkg 在安装包时实际上会 output 用法示例。

以下是设置 vcpkg 工具链的预设文件示例:

{
    "version": 2,
    "cmakeMinimumRequired": {
        "major": 3,
        "minor": 22,
        "patch": 0
    },
    "configurePresets": [
        {
            "name": "cmake-pedantic",
            "hidden": true,
            "warnings": {
                "dev": false,
                "deprecated": true,
                "uninitialized": true,
                "unusedCli": true,
                "systemVars": false
            },
            "errors": {
                "dev": false,
                "deprecated": true
            }
        },
        {
            "name": "generator-ninja",
            "hidden": true,
            "generator": "Ninja Multi-Config",
            "binaryDir": "${sourceDir}/build",
            "cacheVariables": {
                "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
                "CMAKE_DEBUG_POSTFIX": "d",
                "CMAKE_MAKE_PROGRAM": "ninja"
            }
        },
        {
            "name": "dev",
            "displayName": "Development",
            "description": "Development preset",
            "inherits": ["generator-ninja"]
        }
    ],
    "buildPresets": [
        {
            "name": "dev-debug",
            "displayName": "Debug",
            "description": "Build with debug informations",
            "configuration": "Debug",
            "configurePreset": "dev"
        },
        {
            "name": "dev-relwithdebinfo",
            "displayName": "RelWithDebInfo",
            "description": "Build with debug informations and optimizations enabled",
            "configuration": "RelWithDebInfo",
            "configurePreset": "dev"
        },
        {
            "name": "dev-release",
            "displayName": "Release",
            "description": "Build with optimizations enabled",
            "configuration": "Release",
            "configurePreset": "dev"
        }
    ],
    "testPresets": [
        {
            "name": "base-test",
            "hidden": true,
            "output": {
                "outputOnFailure": true
            },
            "execution": {
                "noTestsAction": "error",
                "stopOnFailure": true
            }
        },
        {
            "name": "dev-debug",
            "displayName": "Debug",
            "configuration": "Debug",
            "configurePreset": "dev",
            "inherits": "base-test"
        },
        {
            "name": "dev-relwithdebinfo",
            "displayName": "RelWithDebInfo",
            "configuration": "RelWithDebInfo",
            "configurePreset": "dev",
            "inherits": "base-test"
        },
        {
            "name": "dev-release",
            "displayName": "Release",
            "configuration": "Release",
            "configurePreset": "dev",
            "inherits": "base-test"
        }
    ]
}

由于存在预设文件,您必须改用此命令行:

# from the source directory
cmake --preset dev
cmake --build --preset dev-debug

请参阅cmake-presets文档


不建议

您还可以在 CMake 文件中设置工具链文件。 但是,这样做是不明智的。 从长远来看,这会产生问题。 您应该使您的构建规范独立于您的工具链文件。

在多个平台和多个编译器上构建应用程序时,工具链文件很有用。 硬编码它是在追逐问题。

方法如下:

# This will make issues in the future
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")

在调用project之前执行此操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM