我想从cmake脚本中为vcpgk
选择三元组。但是,我遇到了两个问题。
VCPKG_TARGET_TRIPLET
之后设置project()
cmake_minimum_required(VERSION 3.5)
project(xxx CXX)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
message(STATUS "----------------- x64")
set(__ARCH "x64")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
message(STATUS "----------------- x86")
set(__ARCH "x86")
else()
set(__ARCH "xUNKNOWN")
message(STATUS "----------------- xUNKNOWN")
endif()
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(VCPKG_TARGET_TRIPLET "${__ARCH}-windows-static-md" CACHE STRING "target triplet" FORCE)
message(STATUS "Target triplet for debug is " ${VCPKG_TARGET_TRIPLET})
else()
set(VCPKG_TARGET_TRIPLET "${__ARCH}-windows-static" CACHE STRING "target triplet" FORCE)
message(STATUS "Target triplet for release is " ${VCPKG_TARGET_TRIPLET})
endif(CMAKE_BUILD_TYPE MATCHES Debug)
if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
endif()
find_package(Protobuf REQUIRED)
我遇到错误
CMake Error at C:/Program Files/CMake/share/cmake-3.12/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find Protobuf (missing: Protobuf_LIBRARIES Protobuf_INCLUDE_DIR)
VCPKG_TARGET_TRIPLET
之前设置project()
,它会按预期运行,但是我无法确定它是x64还是x86 cmake_minimum_required(VERSION 3.5)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
message(STATUS "----------------- x64")
set(__ARCH "x64")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
message(STATUS "----------------- x86")
set(__ARCH "x86")
else()
set(__ARCH "xUNKNOWN")
message(STATUS "----------------- xUNKNOWN")
endif()
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(VCPKG_TARGET_TRIPLET "${__ARCH}-windows-static-md" CACHE STRING "target triplet" FORCE)
message(STATUS "Target triplet for debug is " ${VCPKG_TARGET_TRIPLET})
else()
set(VCPKG_TARGET_TRIPLET "${__ARCH}-windows-static" CACHE STRING "target triplet" FORCE)
message(STATUS "Target triplet for release is " ${VCPKG_TARGET_TRIPLET})
endif(CMAKE_BUILD_TYPE MATCHES Debug)
if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
endif()
project(xxx CXX)
find_package(Protobuf REQUIRED)
并获得此打印文件
----------------- xUNKNOWN
PS:由于环境原因,我无法使用-DVCPKG_TARGET_TRIPLET=<>
。局限性。必须在配置过程中检测到
答案 0 :(得分:0)
也许不是最好的解决方案,但最后我通过编译器检查了拱形
if(CMAKE_CXX_COMPILER MATCHES "x64/cl.exe")
set(__ARCH "x64")
elseif(CMAKE_CXX_COMPILER MATCHES "x86/cl.exe")
set(__ARCH "x86")
else()
set(__ARCH "xUNKNOWN")
message(ERRRO "Can't determine target architecture")
endif()
#now setting triplet
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(VCPKG_TARGET_TRIPLET "${__ARCH}-windows-static-md" CACHE STRING "target triplet" FORCE)
message(STATUS "Target triplet for debug is " ${VCPKG_TARGET_TRIPLET})
else()
set(VCPKG_TARGET_TRIPLET "${__ARCH}-windows-static" CACHE STRING "target triplet" FORCE)
message(STATUS "Target triplet for release is " ${VCPKG_TARGET_TRIPLET})
endif(CMAKE_BUILD_TYPE MATCHES Debug)
if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
endif()
#and only now setup the project with configured triplet
project(xxx CXX)
这对Windows MSVC来说对我来说很好。