Search code examples
qmake

How to add a hash character (#) to a qmake variable


I want to write a configuration file with qmake that #defines a few values. But I cannot simply create variables that contain the hash or pound character (#). Nonworking example:

lines = "/* Autogenerated: do not edit */"
if(foo): lines += "#define MYLIB_WITH_FOO 1"
else:    lines += "#define MYLIB_WITH_FOO 0"
write_file(config.h, lines)

The hash starts a comment (inside the string!), so this won't work. How to generate the proper #defines for write_file under qmake?


Solution

  • There's a predefined variable called LITERAL_HASH specially created to deal with this problem.

    If this name seems too long you can create one of your own:

    H = $$LITERAL_HASH
    lines = "/* Autogenerated: do not edit */"
    if(foo): lines += "$${H}define MYLIB_WITH_FOO 1"
    else:    lines += "$${H}define MYLIB_WITH_FOO 0"
    write_file(config.h, lines)