6

Turns out static linking was working, but only for Qt libraries. My 3rd party library QtSerialPort is not linking statically. After some reasearch, I've found that I either have to build this library statically or I have to link directly to a .pri file in my .pro file.

I'm not sure how to do either since it seems QtSerialPort has not been designed for static linking.

The .pri method I really don't understand and has been vaguely described in these two links: http://qt-project.org/forums/viewthread/15223 http://www.qtcentre.org/archive/index.php/t-54505.html

Does anyone have any adivce on how to get either of these methods to work? Or possibly another method?

Also, MSVCP100.dll is not linking statically if anyone could give me any advice on that.

==================================================================================

I am trying to get Qt to statically link libraries so that I can make a standalone application. I have followed various tutorials on how to build Qt statically then building a static application but I am not having much luck. I believe I have succesfully built Qt with static linking because the application has grown in size from 79KB to 7+MB but I am still getting errors saying QtCore4.dll and QtSerialPort.dll are missing. Also, another issue I'm having when using this static configuration, which isn't too serious, is that when I close my program Windows thinks it has crashed and gives me a window saying MyProgram.exe has stopped working...

I am on a Windows machine using MSVC 2010 with Qt 4.8.5 and am using the third party library QtSerialPort.

What I've done accoring to the guides I've been reading is:

Download and extract qt-everywhere-opensource-src-4.8.5.zip Open /mkspec/mwin32-msvc2010/qmake.conf and change the follwing lines to

CONFIG += qt warn_on release incremental flat link_prl precompile_header autogen_precompile_source copy_dir_files debug_and_release debug_and_release_target

and

QMAKE_CLFAGS_RELEASE = -O2 -MT

I then open the MSVC2010 command prompt and cd to this . I then enter the commands

configure -static -release -platform win32-msvc2010
nmake sub-src

After this is done I open my project and add

CONFIG += static

to the .pro file. In QtCreator I then go into Projects, Manage Kits then to Qt Versions and browse to the qMake I just generated. I add a new Kit with this version of qMake. I then clean all and switch to this new kit and run qmake from QtCreator. I then use msvc2010 command prompt to go to the directory where the files are generated and then

nmake release

This generates a rather large .exe but like I said, it's still depending on a couple .dll's.

László Papp
  • 51,870
  • 39
  • 111
  • 135
awpitt13
  • 155
  • 9
  • 1
    Running http://dependencywalker.com/ on your exe might clarify why you have a dll dependency. – Dan O Dec 12 '13 at 02:10
  • 1
    Upvoted. QT deployment can be a challenge. – It'sPete Dec 12 '13 at 10:06
  • 1
    I am not sure what the problem is. You can build QtSerialPort statically like any make based project. What exactly is the problem? – László Papp Dec 14 '13 at 15:38
  • Have you resolved this issue? – László Papp Dec 24 '13 at 08:30
  • Laszlo, when I try to build QtSerialPort statically I get error messages when compiling. I will have to reproduce the error and add it here when I do because I have forgotten what it was. I built QT 4.8.5 statically then tried to build QtSerialPort with that static qmake. It wouldnt work. In the meantime, to get my code working, I built Qt 5 statically which already has the QtSerialPort library and used that for my static build. I would still like to figure out this issue though, so I will get back to you. Thank you Laszlo. – awpitt13 Jan 07 '14 at 18:45

1 Answers1

-1

For static linking of external library one have a couple options, both have their pros and cons.

I. Compile the library for static linking yourself. Link to it.

  1. Look for possible existing configuration switches for static linking. There can be something like QTSERIALPORT_STATIC = no, etc. in the library's .pro/.pri files. Just say yes for the library to compile for static linking and go to the step 4!

  2. In .pro/.pri file replace CONFIG += dll with CONFIG += static.

  3. Remove export declarations from the library. Typically Qt library symbols are declared with some definition like QTSERIALPORT_EXPORT which expands to Q_DECL_EXPORT/Q_DECL_IMPORT in shared library build / its header files usage when linking. You'll need to find where this QTSERIALPORT_EXPORT is defined and replace it with empty definition:

    #define QTSERIALPORT_EXPORT // in source file
    

    or

    DEFINES += QTSERIALPORT_EXPORT # in .pro/.pri file
    
  4. Build the library.

  5. Link to the library .lib/.a file, use the library header files for symbol declarations in your project.

II. Include the library source files into your project and compile them within it (no linking at all).

  1. Include all the source files of the library into your project (add to SOURCES in qmake project file)

  2. Determine all the stuff the library depends on (other libraries, Qt options, etc.) and include it also into your .pro file. OR Include the proper .pri file into your .pro if the library author provides it for in-project compilation (i.e. include(QtSerialPort.pri) or something.)

  3. Remove export/import declarations from the library source code — as described in the item 3 of part I.

  4. Build your project.

Dmitry Markin
  • 1,145
  • 8
  • 8
  • 1
    You are talking about how to link statically, but the OP had a special issue CONFIG += static as he mentioned. I do not really see how you are trying to address that issue at hand. – László Papp Jan 03 '14 at 19:21
  • 1
    Yes, @LaszloPapp, I'm addressing that same issue. The OP wants to link statically, but doesn't know how. – Dmitry Markin Jan 03 '14 at 19:27
  • 1
    He already mentioned how. The problem he was facing, it did not work, and he did not know why. FWIW, our QtSerialPort library has not been tested much for static linking, so it may even be a bug in our software. – László Papp Jan 03 '14 at 19:28
  • 1
    "After this is done I open my project and add CONFIG += static". Which is exactly what has to be done, but it does not work for him. You have **NOT** addressed why that does not work for him which is the main issue in his question. Even I could not address that without further information as one of the contributors to the library. :) – László Papp Jan 03 '14 at 19:30
  • He opens **his** project, not QtSerialPort's project. – Dmitry Markin Jan 03 '14 at 19:31
  • 1
    Which clearly means if he knows how to link his libraries statically, he knows how to do that for QtSerialPort as well. Moreover, configure will also already take care of the static QtSerialPort. Your answer does not try to address the issue, just adds a wall of text which is not relevant. – László Papp Jan 03 '14 at 19:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44446/discussion-between-dmitry-markin-and-laszlo-papp) – Dmitry Markin Jan 03 '14 at 19:36