Set preprocessor definitions from file content in Visual C++

柔情痞子 提交于 2019-12-20 04:53:28

问题


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:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/22503430/set-preprocessor-definitions-from-file-content-in-visual-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!