Search code examples
c++visual-studiovisual-studio-2022

$(ProjectDir) doesn't resolve to actual project


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.


Solution

  • $(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.