Search code examples
c++qtqmakepwdprojects-and-solutions

Understanding the Term \\\"$$PWD/\\\" in qmake project file


I found this line in a qmake project file:

DEFINES += SRCDIR=\\\"$$PWD/\\\"

I know that the PWD-part stands for print working directory and I let give me the value of SRCDIR:

std::cout << "SRCDIR :" << SRCDIR << std::end;

I also changed the variable to SRCDIR=$$PWD but it will not work.

But why we need a slash /, backslahses \ and quotation marks " to get the PWD?


Solution

  • qmake will handle quote characters and backslashes specially.

    Your end goal is to have a string literal defined to be the expansion of SRCDIR. String literals in C++ are contained within double quotes, so you need those quotes to make it through to the compiler as part of the definition.

    In other words, you want the equivalent of:

    #define SRCDIR "somedir/"
    

    where somedir is the current working directory, in this example. To achieve this, you want to escape the quote (so it isn't handled specially by qmake) using \". This stands at both ends of the string.

    Now, what about the escaped backslash, \\? Well, this further escapes the quote from shell processing. When the command

    cc -DSRCDIR="somedir/"
    

    is passed to the shell, the quotes will be stripped as part of the shell's processing. To make sure those quotes remain, and define a string literal, you need to escape them with a backslash at this level, too. The shell will convert \" into ". So the full escape sequence for a double quote character in this case is:

    \\\"
    

    This token appears at both ends of the defined string. The forward-slash just makes it easier to use the path inside the code; it eliminates the need to add a / everywhere you use the path.

    The command that the shell sees will look like

    cc -DSRCDIR=\"somedir/\"
    

    and the definition of SRCDIR inside the compiler will be a string literal, equivalent to the following definition in-source:

    #define SRCDIR "somedir/"