Search code examples
qtqmake

How to provide linker options when linking a static library with qmake?


I want to provide options to the linker when building a static library using qmake. Say I'd want to get verbose linker output when building with MSVC. The project file looks as follows:

# mylib.pro
TEMPLATE = lib
TARGET = mylib
CONFIG += staticlib
QT += core
win32-msvc*: QMAKE_LFLAGS += /VERBOSE
unix: QMAKE_LFLAGS += -v

That's the entire project file. It should result in an empty static library with no objects in it.

Setting neither QMAKE_LFLAGS nor QMAKE_LFLAGS_STATIC_LIB nor LIBS has any effect on the linker. Nothing set in those variables even makes it to the Makefile. If QMAKE_LFLAGS worked, I'd expect to see /VERBOSE or -v passed to the linker on the command line, as appropriate for given platform.

It doesn't matter what makefile generator is used, this behavior seems to be consistent. The two platforms of interest are.

qmake -spec win32-msvc2008
qmake -spec macx-llvm

Due to cross-platform nature of qmake, you can test it on any platform where you happen to have Qt installed. This reproduces on qmake from both Qt 4.8.4 and 5.1.1. The msvc version given in the mkspec doesn't matter.


Solution

  • In staticlib projects, the LFLAGS are not passed to the linker. In fact, there's no documented way to pass such flags.

    The solution is generator-dependent.

    For msvc_nmake, LIBFLAGS are passed to the linker instead. To get verbose output, you might add

    QMAKE_LIBFLAGS += /VERBOSE
    

    To verify that it works, on any system, you can invoke qmake -spec win32-msvc2008; the particular msvc version doesn't matter.

    For unixmake, AR is used to invoke the linker, so you have to add the flags to QMAKE_AR. To get verbose output, you might add

    QMAKE_AR += -v
    

    To verify, invoke qmake -spec macx-llvm; any other unix spec should work as well.