I am using Visual Studio for writing in c++. I try to use $(ProjectDir) macro to access the project directory
auto vertShaderCode = readFile("$(ProjectDir)shaders/vert.spv");
And unfortunately the macro doesn't resolve to actual path, but rather stays exactly as "$(ProjectDir).
ChatGPT suggested writing like this:
std::string projectPath = "$(SolutiontDir)";
auto vertShaderCode = readFile(projectPath + "shaders/vert.spv");
But it doesn't work as well. I have specifically added this macro in "Additional Include Directories" in project properties, but it still stays the same.
$(ProjectDir)
is an MSBuild macro, if you want to use it in code then you'll need to transfer its value as preprocessor macro in project settings MY_PROJECT_DIR="$(ProjectDir)"
and then change code to readFile(MY_PROJECTS_DIR "shaders/vert.spv");
. You may also need to deal with escape sequences by using RAW string literal MY_PROJECT_DIR=R"($(ProjectDir))"
for example, but that's another story.
The MY_PROJECT_DIR="$(ProjectDir)"
solution doesn't work for me. I get a Error C2001 newline in constant
error.
The error happens because the $(ProjectDir)
gets expanded to something like : D:\YourProjectDirectory\
and so then your macro definition becomes: MY_PROJECT_DIR="D:\YourProjectDirectory\"
, and the problem is in the \"
at the end there: it makes the compiler think that you escaped the "
character and that you want the "
character inside the string, not to end the string. That's why it doesn't work. So instead what you need to do is to make it a raw string literal.
What worked for me is:
ProjectDir=R"$(ProjectDir)"
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