3

That's right I want to supply a path as preprocessor define (properties->configuration->c/c++->preprocessor)

MY_PATH=c:\$(WindowsSdkDir)\um

But this hits me upon use with

E1696 cannot open source file "C:\asdf\u0000m\xyz.h"
E0992 command-line error: invalid macro definition: MY_PATH=c:\asdf\um

Because visual studio seemingly sees \u as a unicode escape. However, there is no way to escape the backslash, so now I cant specify any path that contains a directory starting on u. I also cant switch to / as a path separator because I pull in environment variables that use .

What to do?

I am on latest Windows 10 with latest SDK and Visual Studio 2019.

bernd feinman
  • 324
  • 2
  • 11

2 Answers2

2

You should use raw string literals for anything that requires escaping instead

char const * ddd = R"(C:\asdf\u0000m\xyz.h)";

No more escaping required and the result is much more readable. So in this case on the command line you'll use

 -DMY_PATH=R\"\(C:\\asdf\\u0000m\\xyz.h\)\"

because you only need to escape for the shell and not the C++ source code

Demo on Godbolt

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Thanks! But it seems this isnt supported when I need to add the ppdefine to my .vcxproj or use the built-in property editor in VS... Also I need to resolve environment variables in the path... – bernd feinman Aug 18 '22 at 11:24
  • 2
    @berndfeinman Entering `MY_PATH=R"(c:\$(WindowsSdkDir)\um)"` in the properties in Visual Studio works for me. Printing `std::string s = MY_PATH;` results in `c:\C:\Program Files (x86)\Windows Kits\10\\um`, as expected. (Naturally, prepending "c:\" makes no real sense, but that is what you wrote in the original post.) In the vcprojx file, it looks the same: `MY_PATH=R"(c:\$(WindowsSdkDir)\um)";%(PreprocessorDefinitions)`. If it doesn't work for you, you need to post more information (e.g. the relevant part of the vcprojx and the source). – Sedenion Aug 18 '22 at 19:41
  • 1
    Thanks! I am getting the same issue though - but! meanwhile I have found out the bug is related to msvc two phase lookup. So sometimes you can get lucky and it will magically work with "rebuild". Even better turning off /Zc:twoPhase- ... I will report more tomorrow. Meanwhile, can you try to hit rebuild a couple of times or build new with minor modification on your end and see if it works always? – bernd feinman Aug 18 '22 at 19:58
  • It's a header name. Header names are *not* string literals though they look pretty similar. – n. m. could be an AI Aug 19 '22 at 20:31
1

Use four backslashes: -DMY_PATH=\"C:\\\\asdf\\\\u0000m\\\\xyz.h\"

https://godbolt.org/z/9Yn4csjv1

Osyotr
  • 784
  • 1
  • 7
  • 20
  • Thanks, but this still doesnt work. Perhaps unclear - the 0000 are inserted by VS mistakenly, the actual directory name is "um". If i rename it "vm" it works fine. With four slashes, the 0000 are still inserted... I need to put the ppdefine inside my .vcxproj. – bernd feinman Aug 18 '22 at 11:26
  • Hmm that sounds like file encoding problem. Make sure your vcxproj is saved in utf-8 – Osyotr Aug 18 '22 at 12:17
  • It is. It even happens when I input the path with the VS GUI. Seems like a VS bug? – bernd feinman Aug 18 '22 at 12:22
  • Yeah, I bet it is – Osyotr Aug 18 '22 at 12:48