简体   繁体   中英

Static build Qt+Qbs

I create a simple console application "Hello world". First, I compile it with qmake: hello.pro

QT += core
QT -= gui

CONFIG += c++11

TARGET = hello
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

The application is builded normally and on a system without installed Qt and MinGW work fine. The size of the executable is 3.58MB.

Next, we compile the same source code using the QBS build system. Hello.qbs:

import qbs

 CppApplication {
     Depends{
         name: "Qt"
         submodules: [
             "core",
         ]
     }
     name: "HelloWorld-minimal"
     files: "main.cpp"
 }

The executable file size is 4.35MB. The application requires "libwinthread-1.dll", "libstdc ++ - 6.dll" and "libgcc_s_dw2-1.dll".

A question : how correctly to build a static application in QBS with static linking of the above libraries and so that the size of the executable file was the same?

(With the standard build of the project, without statics, the sizes of executable files with Qmake and with Qbs are the same).

Answer was found:

import qbs

CppApplication {
     Depends{
         name: "Qt"
         submodules: [
             "core",
         ]
     }
     Properties {
         condition: Qt.core.staticBuild
         cpp.linkerFlags: [
             "-static",
             "-static-libgcc"
         ]
     }
     name: "HelloWorld-minimal"
     files: "main.cpp"
}

The file size remains larger than when you compile with qmake (I assume that this is due to the fine-tuning of qbs). However, the main problem is solved : the application does not require additional dll's.

UPD: this solution work for QBS 1.6.0. For newest version (1.9.0) linker faild with error:

unrecognized -a option `tic-libgcc'

WTF?

SOLUTION: For QBS 1.9.0 you must used next code:

import qbs

CppApplication {
    Depends{
        name: "Qt"
        submodules: [
            "core",
        ]
    }

    Properties {
         condition: Qt.core.staticBuild
         cpp.driverFlags: [
             "-static",
             "-static-libgcc",
        ]
    }
    name: "HelloWorld-minimal"
    files: "main.cpp"
}
  1. The two project files are not entirely corresponding. You should add:

consoleApplication: true // CONFIG += console
cpp.cxxLanguageVersion: "c++11" // CONFIG += c++11

(I'm ignoring the bundle settings as you are apparently on Windows.)

  1. Are you sure that you are building in the same mode (debug vs release)? Note that with qbs, the default is debug. Set qbs.buildVariant to "release" on the command line if you want to change that.

  2. Comparing the linker command lines produced by qmake and qbs should give you an insight into which libraries get pulled in and how. Use qbs' --command-echo-mode option to see the full command lines used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM