How to get $ (ProjectDir) path in Visual C ++ Unit Testing using CppUnitTestFramework?

I need to be able to get the path to the project directory for my unit testing in order to load some of the files needed for testing. I don't want to hard-code it in case the structure of the solution and absolute paths change in the future.

+3


source to share


1 answer


Ok, this is how I did it:

  • In my project Properties -> Configuration -> C / C ++ -> Preprocessor I added this preprocessor definition UNITTESTPRJ="$(ProjectDir)."

  • Then in my cpp file I did:

#define STRINGIFY(x) #x



#define EXPAND(x) STRINGIFY(x)

string s = EXPAND(UNITTESTPRJ);
s.erase(0, 1); // erase the first quote
s.erase(s.size() - 2); // erase the last quote and the dot
string my_project_dir = s;

      

Silly .

at the end was necessary to avoid ending up \"

in the project directory.

+5


source







All Articles