Search code examples
visual-studio-2015visual-studio-2013nmake

How to escape a double quote in nmake


I'm trying to pass a string literal via a /D flag to the compiler via an nmake script in MS Visual Studio 2013+. The basic pattern is:

CPPFLAGS=/DSOME_STRING_VAR="asdf"

main.exe: main.cpp
    $(CPP) $(CPPFLAGS) # ...

And inside main.cpp I'd like to be able to do something like:

const char *some_string_var = SOME_STRING_VAR;

So far, every variation of this I try results in something like:

main.cpp(271) : error C2065: 'asdf' : undeclared identifier

I've tried one double-quote ", escaping with a caret ^", multiple-escaping the caret ^^^", and doubling up the double-qoute "". All of these are, as far as I can tell, escape syntaxes for batch scripts, but they don't seem to work in nmake.

Is there a way to escape double quotes in nmake so that they get properly passed into the compile command?

For bonus points, are nmake command executed by a shell such at batch? I.e. do I have to worry about one or two levels of escaping here?

I'd be happy to support Visual Studio 2015+ if there were a cleaner solution enabled by the newer version.


Solution

  • tldr: Escape the double-quotes with a backslash.


    If your test file main.cpp is:

    #include <iostream>
    int main() {
      const char *some_string_var = SOME_STRING_VAR;
      std::cout << some_string_var << std::endl;
    }
    

    then the command:

    CL /DSOME_STRING_VAR=\"asdf\" main.cpp
    

    will work as desired, with the test program giving:

    >.\main.exe
    asdf
    

    And if your makefile is the single line:

    CPPFLAGS=/DSOME_STRING_VAR=\"asdf\"
    

    then:

    >nmake main.exe
    

    will also work.

    I was not able to find any reference for this. Tested on VS2013 and VS2017.