Accessing Visual Studio macros from source code?

2020-07-03 04:31发布

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");

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)

6条回答
仙女界的扛把子
2楼-- · 2020-07-03 05:11

Not that I know of but I have an alternative.

Deploy your file as a post build step. In this step you can consume the $(OutDir) macro which represents your binaries drop folder. This should help you place this file at a relative position from your app and use that relative position within your code.

This will also be a lasting solution rather than something done temporarily.

查看更多
我想做一个坏孩纸
3楼-- · 2020-07-03 05:16

You cannot do this automatically, but you can pass specific MSBuild properties to the preprocessor:

<ItemDefinitionGroup>
  <ClCompile>
    <PreprocessorDefinitions>TARGET_DIRECTORY="$(TargetDirectory)"</PreprocessorDefinitions>
  </ClCompile>
</ItemDefinitionGroup>

This can be configured in the IDE by going to the Project Property Pages dialog, browsing to Configuration Properties -> C/C++ -> Preprocessor Definitions, and adding

TARGET_DIRECTORY="$(TargetDirectory)"

Note that your use of + for string literal concatenation is incorrect: string literals (and C Strings in general) cannot be concatenated using +. Rather, string literals can be concatenated simply by placing them adjacent to each other. For example,

TARGET_DIRECTORY "..\\..\\abc.osg"
查看更多
姐就是有狂的资本
4楼-- · 2020-07-03 05:21

use TARGET_DIRECTORY=""$(TargetDir)"" instead of TARGET_DIRECTORY="$(TargetDir)" for string macro. (Note double quotes)

Worked for me in VS2005.

查看更多
何必那么认真
5楼-- · 2020-07-03 05:22

Inside Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor you can add your directive.

PROJECT_DIR=R"($(ProjectDir))"

In code you can use it like.

std::string com = "powershell groovy "PROJECT_DIR"generate_report.groovy " + EXE_DIR;

EXE_DIR is a string in our case.

查看更多
可以哭但决不认输i
6楼-- · 2020-07-03 05:24

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

TARGET_DIRECTORY=LR"($(TargetDir))"

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.

Important!

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))|"

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

查看更多
Lonely孤独者°
7楼-- · 2020-07-03 05:26

I'd suggest making these relative to the application's working directory, or something. Perhaps check out the GetCurrentDirectory function, at http://msdn.microsoft.com/en-us/library/aa364934%28v=vs.85%29.aspx.

查看更多
登录 后发表回答