May 9, 2011

Stian Andre Olsen Stian Andre Olsen
Lab Rat
17 posts

Creating and using DLLs with the MSVC compiler in Qt Creator

 

For some time I have been using the MinGW toolchain on Windows to create a library and an application that uses the library. Let’s call them MyLibrary and MyApplication. MyLibrary.pro contains:

  1. TEMPLATE = lib
  2. TARGET = MyLibrary

MyApplication.pro contains:
  1. TEMPLATE = app
  2. TARGET = MyApplication
  3. LIBS += -LMyLibraryPath -lMyLibrary

The MinGW toolchain produces MyLibrary.dll and libMyLibrary.a and I get no errors while building the application. Today, I tried to use the MSVC toolchain after installing the Qt SDK 1.1 with Qt 4.7.3 for MSVC 2008. When building the library I get no errors but while building the application I get: LNK1104: cannot open file ‘MyLibrary.lib’. The error message is understandable since building the library did not create the lib file, only the dll file. Why isn’t the lib file created? Should I use a different approach when creating and using a library?

 Signature 

Stian Andre Olsen

6 replies

May 9, 2011

sigrid sigrid
Lab Rat
144 posts

Does importing and exporting the symbols as outlined in the following FAQ:

Creating a library [developer.qt.nokia.com] help? See also the following link to the documentation on the same subject:

http://doc.qt.nokia.com/4.7/sharedlibrary.html

May 9, 2011

Stian Andre Olsen Stian Andre Olsen
Lab Rat
17 posts

To be honest, I haven’t tried the exporting and importing. For some reason it works perfect with MinGW without any additional export and import keywords in the source code. Maybe this is the reason it doesn’t work with MSVC. If so, I think I’ll stick with the MinGW toolchain for now. However, it would be nice if someone could explain to me why it works with MinGW and not with MSVC. After all, it is nice to avoid additional statements and keywords in the source code.

 Signature 

Stian Andre Olsen

May 9, 2011

joonhwan joonhwan
Lab Rat
94 posts

Post from IPhone. google __declspec dllexport and dllimport. There should be at least one exported function , IIRC.

 Signature 

joonhwan at gmail dot com

May 9, 2011

Tobias Hunger Tobias Hunger
Robot Herder
3387 posts

PLease use Q_DECL_IMPORT and Q_DECL_EXPORT in Qt. Those will not break your build on non-MSVC compilers and actually work for gcc, too.

You will need some magic to always have the right one applied. Check e.g. https://qt.gitorious.org/qt-creator/qt-creator/blobs/master/src/libs/utils/utils_global.h for an example. You then need to make sure to define (in this case) QTCREATOR_UTILS_LIB when building the library to export the symbols and leave that define out when building against the library to import them properly.

[EDIT: fixed link, Volker]

May 9, 2011

ZapB ZapB
Ant Farmer
1427 posts

The reason it works for MinGW is that gcc by default does not hide symbols in libraries. You can enable this feature even for gcc by adding:

  1. CONFIG += hide_symbols

to your .pro file.

The effect of the above is to add:

  1. -fvisibility=hidden -fvisibility-inlines-hidden

as arguments to each invocation of g++.

This will actually make your runtime startup a little quicker too since the runtime linker has less work to do (assuming you do not export absolutely everything).

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

May 9, 2011

Stian Andre Olsen Stian Andre Olsen
Lab Rat
17 posts

Thanks a lot for all the good answers. I will definitely start using Q_DECL_EXPORT and Q_DECL_IMPORT in my library.

 Signature 

Stian Andre Olsen

 
  ‹‹ QMessageBox on Linux fails to gain focus when shown for a second+ time on closeEvent      Crash on QHostAddress::setAddress()... Possibly? ››

You must log in to post a reply. Not a member yet? Register here!