Visual Studio - Where to define custom path macros?

Visual Studio

Visual Studio Problem Overview


I just opened someone else's Visual Studio project and in their build properties they have a few custom path macro's they are using for their include and lib directories. The macro names are things like this:

$(MY_WHATEVER_INCLUDE_DIR)

I could manually replace every single macro with the real path, but it would be nice to just use the macros. My question is, where do I define these custom path macros at?

Visual Studio Solutions


Solution 1 - Visual Studio

This link http://msdn.microsoft.com/en-us/library/a2zdt10t(v=vs.90).aspx could interest you. I didn't like the idea of changing my whole system configuration just to build a project. The most interesting part on the page is the last comment :

> This page fails to mention how to get to this dialog: > > From Property Manager, double click on a property page. Click on "User Macros" under "Common Properties" in the tree control.

Solution 2 - Visual Studio

Here the approach is described with pictures: https://sites.google.com/site/pinyotae/Home/visual-studio-visual-c/create-user-defined-environment-variables-macros

In Visual Studio you need to:

  1. Click in the main menu "View", then "Property Manager"
  2. Right-click in the empty space of "Property Manager" window and in the pop-up menu click "Add New Project Property Sheet"
  3. After adding the property sheet, double click it in the Property Manager window and in the tree on the left select property page "User Macros"
  4. Then you can click "Add Macro" button

Here is a tutorial on Project Property Sheets: http://www.dorodnic.com/blog/2014/03/20/visual-studio-macros/

Solution 3 - Visual Studio

You can just define them as os environment variables, which is probably what the original author did.

Solution 4 - Visual Studio

A property sheet is likely to be the right solution; this answer elaborates on @gregseth's rather than seeking to replace it, as it's too long for a comment.

I found that I needed different paths for 32-bit and 64-bit targets, and doing that took a bit of figuring out, so I've documented the process in detail.

One key misunderstanding I had with property sheets was that, unlike the usual VS property editor where you can edit different configuration/platform combos, a property sheet is just a list of properties. It doesn't have per-configuration and per-platform sub-sections. That was confusing because when I added a sheet to a project it appeared under each configuration/platform node rather than under the top level project node. All the entries are actually for the same property sheet file, so editing one changes all of them, but I didn't initially understand that and thought I'd still have to change the value in each place individually.

You can add a property sheet to just one configuration/platform combo, to all of them, or to just some subset.

If you want to have global settings then configuration/platform overrides you can do so by making sure the more specific property sheets are last. So you might have a property sheet "all configurations" then one for "x86", one for "x64" one for "debug" and one for "release". The x64 debug target would have the sheets "all", "x86", "debug". Basically emulating what VS's property editor does internally.

Solution 5 - Visual Studio

Re: hmm.. I dont seem to have the "User Macros" option under "Common Properties". I am using VS 2010 Pro

The User Macros option doesn't show up if you open the property dialog for a proj file, as you do in the normal Files view. You have to switch to the Propery view, expand some project, and choose a Property Page (*.props) that you added for the purpose. The User Macros show up there.

Or, you can just edit the XML directly. Macros work just fine if defined in a .*proj file, but making it a "User Macro" is pointless if there's no edit page. So just make it a plain property in a <PropertyGroup>.

As pointed out earlier, it also pulls in Environment Variables. However, you have to be sure to set them in a context where the Devenv will see them! Do that in a command shell and then run DEVENV from that same command prompt. When I had that situation, I made a batch file to set the proper variables and launch DEVENV, and put that bat file icon on the desktop.

Solution 6 - Visual Studio

Try the other way without the hassle adding to each Property Sheet

Go to Windows OS System Properties > Environment Variables, just New and input the Variable e.g.: MY_PATH and value e.g.: D:\Dev_Path\

after that you have to restart your Visual Studio, you should be able to have ${MY_PATH} in macro list

p/s: just notice Jason Williams answered above is the OS Environment Variables method

Solution 7 - Visual Studio

Same answer as to @Serge Rogatch, except that I was not able to find "Property Manager" in View.

Visual steps for quick navigation: enter image description here

Solution 8 - Visual Studio

In 2019 you can add custom macros to all of your projects by adding a Directory.Build.props xml file at the root of your solution. They don't show up in the macros section, but if referenced as usual with $(MacroName) they are used in the build.

<Project>
 <PropertyGroup>
   <Deterministic>true</Deterministic>
 </PropertyGroup>
</Project>

Solution 9 - Visual Studio

The way for the latest visual studio versions (2015+) Is as follows:

To create a user-defined macro:

  1. In the Property Manager window (on the menu bar, choose View, Property Manager), open the shortcut menu for a property sheet (its name ends in .user) and then choose Properties.
  2. The Property Pages dialog box for that property sheet opens. In the left pane of the dialog box, select User Macros. In the right pane, choose the Add Macro button to open the Add User Macro dialog box.
  3. In the dialog box, specify a name and value for the macro. Optionally, select the Set this macro as an environment variable in the build environment check box.

Source

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJake WilsonView Question on Stackoverflow
Solution 1 - Visual StudiogregsethView Answer on Stackoverflow
Solution 2 - Visual StudioSerge RogatchView Answer on Stackoverflow
Solution 3 - Visual StudioJason WilliamsView Answer on Stackoverflow
Solution 4 - Visual StudioCraig RingerView Answer on Stackoverflow
Solution 5 - Visual StudioJDługoszView Answer on Stackoverflow
Solution 6 - Visual StudiohghewView Answer on Stackoverflow
Solution 7 - Visual StudiouseromView Answer on Stackoverflow
Solution 8 - Visual StudioRalph GastonView Answer on Stackoverflow
Solution 9 - Visual StudioFantastic Mr FoxView Answer on Stackoverflow