在基于QMAKE的(macOS硅) Qt项目上,为了使它找到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?
发布于 2022-10-04 11:18:12
正如指令所述,您不应该更改您的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实际上会输出使用示例。
下面是一个设置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文件中设置工具链文件。然而,这样做是不明智的。从长远来看,这将造成问题。您应该使构建规范独立于您的工具链文件。
在多个平台和多个编译器上构建应用程序时,工具链文件非常有用。硬编码是在追逐问题。
以下是如何做到这一点:
# This will make issues in the future
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
在任何调用project
之前都要这样做。
https://stackoverflow.com/questions/73947030
复制相似问题