I saw a couple of questions relevant to this topic, but I didn't found the easy and common way to do this.
My question is: How can I have $(projectDir)
or another Macro as a string
or char*
in my C++ code?
Thanks
If your compiler supports raw strings then you can add MYMACRO=R"($(ProjectDir))";
to preprocessor definitions (screenshot from Visual Studio 2013):
Then you can add the following to your code:
#pragma message("MYMACRO == " MYMACRO) // will print project dir during compilation
std::cout << MYMACRO << std::endl; //will print project dir at run time
MYMACRO=R"($(ProjectDir))"; prefectly works for me.
I was in need of this for a long time and this came as a perfect solution. My test cases took input file path relative to $(ProjectDir) while running the exe tried to locate the input file path relative to the bin folder. As a result, I could have either the VS project happy or the exe, but not both.
This solution worked perfectly to make both of them happy.
Thanks @mvidelgauz for the valuable info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With