Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio C++ preprocessor-define fails with path starting on "u"

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.

like image 703
bernd feinman Avatar asked Nov 06 '25 07:11

bernd feinman


1 Answers

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

like image 190
phuclv Avatar answered Nov 08 '25 21:11

phuclv