108

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?

Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
  • 2
    Well fudge, I expected this to be really easy to find. After 10 minutes I gave up . . . +1 from me. It seems to have changed since VS 6.0 days . . . any chance your user has these defined directly in their environment? – Frank Merrow Jan 17 '11 at 06:21
  • 1
    gregseth's answer contains the real solution to this problem: there is a User Macro section underneath Common Properties when editing a property sheet, but only if that sheet is specific to the solution/project. So if you're editing the default sheets that are shared across all projects, User Macros won't show. See my comment on gregseth's answer below. – Cam Jackson Mar 30 '12 at 02:27
  • 1
    Using a .props file does it for in Visual Studio 2010 for defining/changing macros. Prior to that, the file extension was .vsprops. – Aditya Kumar Pandey Jul 24 '12 at 09:50
  • 1
    I've written some more details about property shset usage here: http://stackoverflow.com/q/25810603/398670 – Craig Ringer Sep 16 '14 at 06:06

9 Answers9

59

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/

Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
  • 9
    This answer needs more upvotes. Please select this is accepted answer. The current accepted answer doesn't even answer the question. – Mike S Nov 02 '16 at 17:25
  • 1
    In case it isn't obvious (it wasn't to me) you have to add your property sheet to all your projects (assuming you have multiple in the same solution). I couldn't figure out why it wasn't working, but then realised I had added it to a different project. – Matt Dec 04 '19 at 05:03
  • This doesn't work at all. I can't find User Macros in any projects - even projects that already have custom macros. – Bok McDonagh Mar 23 '23 at 15:00
  • dead link is dead – ycomp Jun 13 '23 at 15:01
52

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.

Dan Nissenbaum
  • 13,558
  • 21
  • 105
  • 181
gregseth
  • 12,952
  • 15
  • 63
  • 96
  • 9
    hmm.. I dont seem to have the "User Macros" option under "Common Properties". I am using VS 2010 Pro – Jelle Vergeer Jan 25 '12 at 21:51
  • 22
    @Jelle The larger comment from the above link contains the extra info you need! 'User Macros' will only display under 'Common Properties' if the property sheet that you're editing is specific to the project/solution! So what you need to do is open the Property Manager window in VS, right-click on your project and add a new property sheet. Then right-click->Properties on the newly created sheet, and you will now see the 'User Macros' option underneath 'Common Properties'! Hope that helps :) – Cam Jackson Mar 30 '12 at 02:24
  • 2
    This is quite a hidden option. This is the new link for the MSVS support page (http://msdn.microsoft.com/en-us/library/f2t8ztwy%28v=vs.90%29.aspx), almost as nothing. In this page (https://sites.google.com/site/pinyotae/Home/visual-studio-visual-c/create-user-defined-environment-variables-macros) is a list of steps more detailed, not perfect but gives you some hints. – Javier Mr Feb 06 '14 at 16:42
  • 1
    Added some more info and examples here: http://stackoverflow.com/q/25810603/398670 . One key thing to understand is that property sheets can be attached to all, some, or just one configuration/platform combo. Unlike in the normal properties editor there aren't different values for different configurations/platforms; if you want that, you use different sheets for each. Quite flexible once you figure out how to combine multiple sheets and have one refer to macros in another. – Craig Ringer Sep 16 '14 at 06:08
  • With respect to Visual Studio 2019 and the comment by Doug Kimzey, it is possible to add them in VS 2019. user macros can be added and edited following the procedure indicated by the response by Serge Rogatch. It took me a bit to find it. :) – Hamp Mar 20 '20 at 21:06
  • This page fails to mention how to get to this dialog... mmh great, I have 200 options to search into. – Sandburg Nov 05 '21 at 15:16
13

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

Jason Williams
  • 56,972
  • 11
  • 108
  • 137
9

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.

Community
  • 1
  • 1
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
4

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

userom
  • 403
  • 5
  • 8
3

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.

JDługosz
  • 5,592
  • 3
  • 24
  • 45
3

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>
Ralph Gaston
  • 33
  • 1
  • 7
2

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

Community
  • 1
  • 1
hghew
  • 311
  • 2
  • 4
  • 15
0

It is terribly complicated for such a simple task.

I have rather modifying them directly by hand through the .vcxproj file.

Here is a small example that should be useful

  <PropertyGroup Label="UserMacros">
    <ProtoBuild Condition="'$(ProtoBuild)' == ''">C:\gRPC\grpc\.build</ProtoBuild>
    <Protoc Condition="'$(Protoc)' == ''">$(ProtoBuild)\third_party\protobuf\Release\protoc.exe</Protoc>
  </PropertyGroup>
NGI
  • 852
  • 1
  • 12
  • 31