4

I have a file in my project which contains the project version number. I have to make the content of this file available as a preprocessor definition in my code.

What I need is to somehow pass the value from the file to the compiler as a /D parameter.

I tried to add the preprocessor definition

VERSION=$(Version)

and set the Version environment variable in a prebuild step, but I did not find a way to do the latter, so I got stuck.

1
  • 3
    Auto-generating a .h file with a #define that you #include in a .cpp or .rc file is the simple and boilerplate solution. Controlling a /D compiler option might be possible, depends a lot on how you build, rarely very practical. Commented Mar 19, 2014 at 11:06

3 Answers 3

3

As a workaround, a pre-build step could be created which calls a script that reads the file and generates the macro definition in a header. Then this header file could be included in my projects.

However I do not find this solution nice, I hope there is a better one.

Sign up to request clarification or add additional context in comments.

2 Comments

This is actually how one of my projects deals with versioning. As a prebuild step, it gets a version number from Mercurial and writes a corresponding #define into a header file. The header is included in my project, so the freshly-generated version number is available to me without any extra work required.
I accepted this based on current votes and comments but I hope someone will come up with a more accurate solution.
3

As an alternative to generating .h files, you can also generate a .vsprops (Property Sheet) file. .vsprops files are to .vcproj files what .h files are to .cpp files. In particular, you can define a /D command-line option in a property sheet. This is how /D UNICODE is commonly define, via a default property sheet.

The additional benefit is that you can also set the /VERSION flag for the linker, something which you can't do via a header file.

Comments

0

You can use View > Property Manager > Add New Project Property Sheet option to add a new .props file to your solution which will contain version definition:

Property Pages > User Macros

All macros should be set like this in order for them to be available in your build environment:

Add User Macro VER_STRING

In order to actually use them in code, you will have to define them in Preprocessor Definitions as well:

Preprocessor Definitions

Bear in mind that .props file is neither added to source control nor made a part of the solution by default.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.