Accessing Visual Studio macros from source code?

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.


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"

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.