Qt 3rd Party Library Static Linking (QtSerialPort)

1.8k views Asked by At

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.

1

There are 1 answers

7
Dmitry Markin On

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.