在基于 QMAKE 的(macOS 硅)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 一起使用?
uj5u.com熱心網友回復:
正如說明所說,您根本不應該更改您的 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-presets檔案
不建議
您還可以在 CMake 檔案中設定工具鏈檔案。但是,這樣做是不明智的。從長遠來看,它會產生問題。您應該使您的構建規范獨立于您的工具鏈檔案。
在多個平臺和多個編譯器上構建應用程式時,工具鏈檔案很有用。硬編碼它正在追逐問題。
這是如何做到的:
# This will make issues in the future
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
在呼叫project.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/513277.html
