Link to home
Start Free TrialLog in
Avatar of think-cell
think-cell

asked on

How to convert $(SolutionDir) into a C++ string?

I need to access the compile-time path of the solution/project in C++ code. This seems to be easy to solve by way of compiler option /D ("preprocessor definitions"), which allows to add C++ macros that are treated the same way as regular "#define" statements. Using the /D compiler switch, I can use environment variables such as $(SolutionDir) or $(ProjectName), which are resolved to their actual values before being passed to the preprocessor.

Example: /D _SOLUTIONPATH_="$(SolutionDir)"
Resolves to: #define _SOLUTIONPATH_ "C:\svn\solutions\mysolution\"

Unfortunately, the solution path contains backslashes which create unintended escape sequences in the string that is resolved from the environment variable. I did not find a way to create a properly escaped string. The problem has been discussed elsewhere without any solution:
http://bytes.com/sitemap/t-788402.html

Any of the following would qualify as a solution to my question:
- A way to create a properly escaped C++ string from the $(SolutionDir) environment variable.
- A way to make $(SolutionDir) a path with forward slashes instead of backslashes, which would eliminate the necessity to escape anything.
- A way to define a C++ macro that contains the length of the $(SolutionDir) string, rather than the string itself.
- Any other way to make the compile-time solution path available in the C++ code without going through compiler option /D.
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Really cool! :) Just nice. It is a solution.
Maybe the question just looks so strange and the problem is somewhere in the application design?
Maybe an API like GetModuleFileName / GetCurrentDirectory could help?
 
BTW, set the working folder in the project settings as the $(SolutionDir) and then from the code call GetCurrentDirectory. Or $(SolutionDir)Bin.
But, I'd say, this folder name is always known - predefined/calculated somehow.
@pgnatyuk: Please re-read the question - think-cell wants to have the value of the macro $(OutDir) as a string at compile-time, not at run-time ...
Avatar of think-cell
think-cell

ASKER

Crazy, and works! Thanks.

Why would you use the compiler switch to include the file? I use a regular include statement next to the only place where I refer to the _SOLUTION_PATH_.
You're welcome, I'm glad I could help ...

And, I used this command line option for include so it 'feels' as if the macro was declared through preprocessor-settings - of course you can even include the created file instead using #include's where you need it.

Have a nice day,

best regards,

ZOPPO