从源代码访问Visual Studio宏? [英] Accessing Visual Studio macros from source code?

查看:93
本文介绍了从源代码访问Visual Studio宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio具有诸如$(TargetDirectory)$(OutputPath)等的宏.
在我的源代码中,我想指定一个相对路径,用于从TargetDirectory下面几级以下的文件夹中加载文件. 目前,我正在执行以下操作:mLayer = mEngine->AddLayer("D:\\Projects\\abc.osg");,我希望它类似于mLayer = mEngine->AddLayer(($TargetDirectory)+"..\\..\\abc.osg");

Visual Studio has macros like $(TargetDirectory), $(OutputPath) etc.
In my source code, I want to specify a relative path for the loading of a file from a folder a few levels below the TargetDirectory.
Currently I'm doing this: mLayer = mEngine->AddLayer("D:\\Projects\\abc.osg"); and I want it to be something like mLayer = mEngine->AddLayer(($TargetDirectory)+"..\\..\\abc.osg");

这只是一个临时要求,因此我可以将代码提供给某人以进行小型演示,并且他的TargetDirectory与目录的对齐方式不同.有什么办法可以在源代码中使用Visual Studio宏? (至少我知道可以访问系统环境变量)

It's just a temporary requirement, so that I can give my code to a person for a small demo, and his TargetDirectory is differently aligned wrt my directories. Is there any way to make use of the Visual Studio macros in source code? (at least I know that System environment variables can be accessed)

推荐答案

转到项目属性->配置属性-> C/C ++->预处理程序->预处理程序定义,然后添加以下内容:

Go to Project Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions and add the following:

TARGET_DIRECTORY=LR"($(TargetDir))"

这定义了一个名为TARGET_DIRECTORY的宽字符串文字,其中包含$(TargetDir)宏的内容.这里重要的是,这将创建一个C ++原始字符串,该字符串不会将反斜杠视为转义字符.路径包含反斜杠.使用常规的字符串文字将是不正确的,甚至在某些情况下甚至会导致编译器错误.

This defines a wide string literal named TARGET_DIRECTORY that contains the contents of the $(TargetDir) macro. The important thing here is that this creates a C++ raw string that does not treat backslashes as escape characters. Paths contain backslashes. Using a regular string literal would be incorrect and would even give you compiler errors in some cases.

重要!

如果您使用的宏可能包含右括号,并在双引号()后加上",则必须使用其他定界符,该定界符不能出现在宏值中,例如:

If you use a macro that may contain a closing parenthesis followed by double quotation marks )" you must use an additional delimiter, that cannot occur in the macro value, for example:

TARGET_DIRECTORY=LR"|($(TargetDir))|"

对于Windows文件系统路径,则没有必要,因为路径不能包含双引号.

In the case of windows file system paths this is not necessary because paths cannot contain double quotation marks.

这篇关于从源代码访问Visual Studio宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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