Search code examples
c++visual-studiovisual-c++c-preprocessor

Set preprocessor definitions from file content in Visual C++


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.


Solution

  • 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.