The Ultimate Qt Community site
Home News Forum Wiki Contest FAQ Links

Building static applications

From QtCentreWiki

Jump to: navigation, search

Contents

[edit] Introduction

In static linking, the linker embeds all library routines used in an application into the executable. This leads to a significantly larger executable compared to dynamic linking. Static linking is mostly used for simplifying deployment, because the executable is more portable since it doesn't require all libraries installed on the target system. Just be aware that static linking has it's drawbacks, see Pros and Cons.

[edit] Building a static version of Qt

In order for static linking to work, we first need to build Qt itself statically. On windows this can be achieved by changing to Qt's root directory and executing:

 configure -static

Any other options you may need should also be appended to the argument list of the confingure script. Afterwards issue following command:

nmake sub-src

or

mingw32-make sub-src

depending on your build-environment.

More thorough descriptions of this procedure are available as separate articles:

[edit] Building a static application

All you need to do is to add one variable to the .pro file:

CONFIG += static

or

CONFIG += staticlib

depending on the type of the project.

[edit] Pros and cons

[edit] (+)

  • A stand-alone executable (less files to deploy),
  • the application might startup/run slightly faster (no need to load dynamic libraries).

[edit] (-)

  • Significantly larger executable (you can reduce its size using UPX or any other packer),
  • no flexibility (installing a new version of a library has no effect on executable which has been statically linked to a specific older version of the same library),
  • you cannot deploy plugins.

[edit] Linux "mostly static"

Under Linux, there are certain libraries that you don't really want to link statically. Furthermore, many systems don't have every single library available as a static version. So I wrote a qmake fragment to solve this issue for me: it links "as statically as possible", except for libraries such as libdl (the library that contains functions such as dlopen) and pthreads. Both of these are fairly dangerous candidates for static linkage. This qmake code uses GCC to query whether or not a static version of each library specified with "-lfoo" exists, and replaces it with a fully qualified path name if it does. Please note that this absolutely requires Qt4, and probably Qt4.2 in the bargain - it's using more than one function from the Undocumented qmake section... I put it in a .prf file in a features directory for my application and can thus reference it as the last line of my .pro file with load().

defineTest(matches) {
  value = $$1
  regex = $$2
  test = $$replace($${value}, $${regex}, "")
  isEmpty($${test}) {
    return(true)
  } else {
    return(false)
  }
}
 
linux-g++* {
   for(lib, LIBS) {
      # If it's something we don't recognize (neither "-lfoo" nor "-Lfoo") just add it directly
      !matches(lib, "^-l.*$") {
         libtemp *= $${lib}
      # Don't statically link POSIX threading or dlOpen libraries
      } else:isEqual(lib, "-lpthread") | isEqual(lib, "-ldl") {
         libtemp *= $${lib}
      # Ask GCC to find a static version of the library
      } else {
         libfile = $$replace(lib, "^-l(.*)$", "lib\1.a")
         libloc = $$system("gcc $${LIBS} -print-file-name=$${libfile}")
         # If it didn't find it, just keep the 
         isEqual(libloc, $${libfile}) {
            libtemp *= $${lib}
         } else {
            libtemp *= $${libloc}
         }
      }
   }
   LIBS = $${libtemp}
   unset(libtemp)
   unset(libfile)
   unset(libloc)
}

I've probably made some unsafe assumptions here but it works for me, and should be a good start towards something "safe".




jpn 21:47, 6 October 2006 (CEST)
gcs 10:47, 1 March 2007 (GMT-0600)

Personal tools