PDA

View Full Version : Understanding how to link dynamic / static library with Qt and MinGW



giorgik
22nd December 2012, 11:52
Hello everyone, I have a bit confused about how to write in the project file .pro for QtCreator to link a dynamic and static library. I use the MinGW compiler (making a Windows).
Suppose you have the following libraries:
1 - Dynamic: vettore.dll
2 - static: matrice.a
Suppose also that the two libraries are in the directory:
1 - C :/ dynamic dynamic library vettore.dll
2 - C :/ static for static library matrice.a

The project file is called test.pro. How should I write it for him to link the libraries to my program that do the test named main.cpp ?

amleto
22nd December 2012, 12:51
LIBS += -Lc:/dynamic -Lc:/static -lvettore -lmatrice

http://doc.qt.digia.com/qt/qmake-project-files.html

for windows you will need a .lib to go with the .dll for linking, I believe

giorgik
22nd December 2012, 18:12
Thank you. To obtain a dynamic library. Lib and. Dll with QtCreator, how should I do? I can write a very simple example (like Hello World) ?

amleto
22nd December 2012, 18:44
using the creator gui:
new project -> c++ library -> Type= Shared library

This gives me:
pro file



QT -= core gui

TARGET = helloworld_dll
TEMPLATE = lib

DEFINES += HELLOWORLD_DLL_LIBRARY

SOURCES += helloworld_dll.cpp

HEADERS += helloworld_dll.h\
helloworld_dll_global.h


header


#ifndef HELLOWORLD_DLL_H
#define HELLOWORLD_DLL_H

#include "helloworld_dll_global.h"

class HELLOWORLD_DLLSHARED_EXPORT Helloworld_dll {
public:
Helloworld_dll();

void print();
};

#endif // HELLOWORLD_DLL_H


source


#include "helloworld_dll.h"

#include <iostream>
Helloworld_dll::Helloworld_dll()
{
}

void Helloworld_dll::print()
{
std::cout << "hi from dll\n";
}


dll defines


#ifndef HELLOWORLD_DLL_GLOBAL_H
#define HELLOWORLD_DLL_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(HELLOWORLD_DLL_LIBRARY)
# define HELLOWORLD_DLLSHARED_EXPORT Q_DECL_EXPORT
#else
# define HELLOWORLD_DLLSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // HELLOWORLD_DLL_GLOBAL_H

giorgik
23rd December 2012, 18:50
thanks amleto, for the sample code. I discovered, however, that it is not enough that automatically creates the wizard, you have in the project file (.pro file), add this line:

CONFIG += dll so the compiler does not find the function main (being a library, there is no main).

ChrisW67
24th December 2012, 04:57
No you don't. The linker will only expect to find a main() if the template type is app. Amletos' example builds cleanly and correctly with MingW.

When you build a "lib" project with you will get a shared library by default on Linux and Windows. All that adding "dll" to CONFIG does is request a shared library again.
If you build the project with the MingW tools there will be a resulting helloworld_dll.dll file and a libhelloworld_dll.a file.
If you build the same project with the Microsoft compiler you will get helloworld_dll.dll and helloworld_dll.lib.
The linkers use information from the .a or .lib file to arrange linking to the the .dll at run time. Only the .dll file is required at run time.

giorgik
26th December 2012, 18:16
Thanks
ChrisW67