Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Visual Studio macros from source code?

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)

like image 680
Nav Avatar asked Feb 15 '13 04:02

Nav


People also ask

Where are macros defined in Visual Studio?

Select Edit and then in the Edit dialog box, choose the Macros button. The current set of properties and macros visible to Visual Studio is listed along with the current value for each.

Does VSCode have macros?

You can run your macros from the command pallette and shortcut keys. You can manage your macros by units of files. You can debug your macro on the debugger of the VSCode.

How do I enable macros in Visual Studio?

Click Options. Click Trust Center, and then click Trust Center Settings. In the Trust Center, click Macro Settings. Check or uncheck Trust access to the VBA project object model to enable or disable access to Visual Basic Projects.


2 Answers

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.

like image 132
Tom Avatar answered Oct 05 '22 17:10

Tom


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"
like image 42
James McNellis Avatar answered Oct 05 '22 18:10

James McNellis