Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting projectDir as a string or char* in c++

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

like image 858
mbaros Avatar asked Jul 05 '16 11:07

mbaros


2 Answers

If your compiler supports raw strings then you can add MYMACRO=R"($(ProjectDir))"; to preprocessor definitions (screenshot from Visual Studio 2013): enter image description here

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
like image 130
mvidelgauz Avatar answered Sep 24 '22 16:09

mvidelgauz


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.

like image 37
chandrashekhar Avatar answered Sep 21 '22 16:09

chandrashekhar