Qt Creator: how to tell win32 from win64

2019-08-02 13:48发布

问题:

I have to do something like this in a .pro file:

win32 {
    LIBS += -L../3rdparty/libusb-win32/lib/msvc -llibusb
} else
win64 {
    LIBS += -L../3rdparty/libusb-win32/lib/msvc_x64 -llibusb
}

The problem is it doesn't work, it always links win32 library. Any suggestions?

回答1:

You can use QT_ARCH variable to detect whether your configuration is 32 or 64 :

contains(QT_ARCH, i386) {
    message("32-bit")
}else {
    message("64-bit")
}

When the target is 32-bit, the variable returns i386 and in case of a 64-bit target it has the value of x86_64.



回答2:

Here's how we do that:

win32 {
win32-g++:contains(QMAKE_HOST.arch, x86_64):{
    LIBS += ... #for win64
} else {
    LIBS += ... #for win32
}
}


回答3:

October 2016 update. The following code works on Windows (at least with all the recent MSVC compilers - didn't test MinGW), Mac OS X (clang) and Linux (GCC). Feel free to omit the first clause and refer to QT_ARCH directly if you don't need Qt 4 support.

greaterThan(QT_MAJOR_VERSION, 4) {
    TARGET_ARCH=$${QT_ARCH}
} else {
    TARGET_ARCH=$${QMAKE_HOST.arch}
}

contains(TARGET_ARCH, x86_64) {
    ARCHITECTURE = x64
} else {
    ARCHITECTURE = x86
}