将“ -D”设置为CMake中的变量 [英] Setting "-D" variables inside CMake

查看:506
本文介绍了将“ -D”设置为CMake中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用vcpkg,我想将变量 VCPKG_TARGET_TRIPLET 设置为我的特定三元组。


此处的帮助 https://vcpkg.readthedocs .io / zh-CN / latest / users / integration /#using-an-environment-variable-in-a-command-line-line-option (页面底部)表示您需要将其作为- CMake的D选项,但我想在CMakeLists.txt中设置它。


我尝试了以下两个选项,但两个选项似乎都不起作用:

  set(VCPKG_TARGET_TRIPLET x64-mingw-static CACHE STRING)#不起作用
set(VCPKG_TARGET_TRIPLET x64-mingw -static)#也不起作用

我想在CMakeLists.txt中而不是命令中执行此操作的原因这行是因为我当前使用VS Code和CMakeTools扩展程序进行设置。 / p>

如何模拟我的CMakeLists中的-D标志?


我还应该提到我的整个项目已由顶层CMakeList组成,作为一种主项目,然后,该子文件夹包含带有另一个CMakeList和我的实际文件夹的子文件夹。项目(使用find_package命令的项目)。

解决方案

尝试使用 add_compile_definitions target_compile_definitions


像这样

  add_compile_definitions (VCPKG_TARGET_TRIPLET = x64-mingw-static)

  target_compile_definitions(目标名称PUBLIC VCPKG_TARGET_TRIPLET = x64-mingw-static)

CMake教程


I am using vcpkg and I would like to set the variable VCPKG_TARGET_TRIPLET to my specific triplet.

The help here https://vcpkg.readthedocs.io/en/latest/users/integration/#using-an-environment-variable-instead-of-a-command-line-option (bottom of the page) says you need to pass it as a -D option to CMake, but I would like to set it in my CMakeLists.txt.

I have tried the following two options, but both do not seem to work:

set(VCPKG_TARGET_TRIPLET "x64-mingw-static" CACHE STRING "") # doesn't work
set(VCPKG_TARGET_TRIPLET "x64-mingw-static") # also doesn't work

The reason I want to do this in CMakeLists.txt and not the command line is because of my current setup with VS Code and the CMakeTools extension.

How can I "simulate" the -D flag inside my CMakeLists?

I should also mention that my whole "project" consists of a top-level CMakeLists as a sort of "master project" which then includes a sub-folder with another CMakeLists and my "actual" project (the one with the find_package commands).

解决方案

Try with add_compile_definitions or target_compile_definitions

Like this

add_compile_definitions(VCPKG_TARGET_TRIPLET="x64-mingw-static") 

or

target_compile_definitions(targetname PUBLIC VCPKG_TARGET_TRIPLET="x64-mingw-static")

CMake Tutorial

这篇关于将“ -D”设置为CMake中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆