PDA

View Full Version : Creating a DLL using Qt



Althor
3rd July 2010, 17:50
Hi!

I am learning to create a DLL which will be used from no-qt apps and I have a doubt.

My dll has only a cpp file:



#include <iostream>
using namespace std;

void hello3() {
cout << "!!!Hello World 3!!!" << endl; // prints !!!Hello World!!!
}

void hello2() {
cout << "!!!Hello World 2!!!" << endl; // prints !!!Hello World!!!
}


If I build the dll with that cpp file then my dll exports the following symbols:


_CRT_MT
_Z6hello2v
_Z6hello3v
_get_output_format


It exports all the functions inside the dll.


But if I add export declaration to one of my functions

void hello3() {
cout << "!!!Hello World 3!!!" << endl; // prints !!!Hello World!!!
}

extern "C" __declspec(dllexport) void hello2() {
cout << "!!!Hello World 2!!!" << endl; // prints !!!Hello World!!!
}

then my dll exports only the symbol:

hello2

It only exports the functions I have delcared explicitily as exported. Why??



Finally, if I add the 'extern "C"' to all my functions

extern "C" void hello3() {
cout << "!!!Hello World 3!!!" << endl; // prints !!!Hello World!!!
}

extern "C" void hello2() {
cout << "!!!Hello World 2!!!" << endl; // prints !!!Hello World!!!
}

then the symbols exported are:


_CRT_MT
_get_output_format
hello2
hello3



I don´t understand why qt has this behaviour.
Can you help me?

Regards.

agathiyaa
3rd July 2010, 18:42
Hi, This is not Qt Behavior. Please learn what is name mangling and when to use extern "C"

Althor
3rd July 2010, 18:47
Hi,

I know what name mangling is and the use of extern c

My two questions are

why does qt export all the functions inside the dll by default?

and why does qt export only the functions with '__declspec(dllexport)' declarations if I use it?

Regards

agathiyaa
3rd July 2010, 19:36
Althor,


why does qt export all the functions inside the dll by default?
1. Qt is not used/responsible to create your dll. It is just a toolkit to create gui's and do other common task in platform independent way.
2. Your compiler creates the dll. In windows you have to use __declspec(dllexport) or use a .def file to export a function from a dll.

The following link may be useful... ?



http://www.flipcode.com/archives/Creating_And_Using_DLLs.shtml
http://www.codeguru.com/cpp/cpp/cpp_mfc/tutorials/article.php/c9855


Creating a DLL using Qt is different from Creating a DLL to use with Qt App.

ChrisW67
4th July 2010, 05:28
I don´t understand why qt has this behaviour.
Can you help me?
Regards.
The behaviour you describe has nothing to do with Qt. This is a behaviour of your compiler and linker. I assume from your conflation of Qt and compiler that you are using the Qt SDK for Windows, which bundles the MingW includes, GNU compiler and tools. From the GNU ld manual (http://sourceware.org/binutils/docs/ld/WIN32.html#WIN32):
_exporting DLL symbols_
The cygwin/mingw `ld' has several ways to export symbols for dll's.

_using auto-export functionality_
By default `ld' exports symbols with the auto-export
functionality, which is controlled by the following command
line options:

* -export-all-symbols [This is the default]
* -exclude-symbols
* -exclude-libs
* -exclude-modules-for-implib
* -version-script

When auto-export is in operation, `ld' will export all the
non-local (global and common) symbols it finds in a DLL, with
the exception of a few symbols known to belong to the
system's runtime and libraries. As it will often not be
desirable to export all of a DLL's symbols, which may include
private functions that are not part of any public interface,
the command-line options listed above may be used to filter
symbols out from the list for exporting. The `--output-def'
option can be used in order to see the final list of exported
symbols with all exclusions taken into effect.

If `--export-all-symbols' is not given explicitly on the
command line, then the default auto-export behavior will be
_disabled_ if either of the following are true:

* A DEF file is used.
* Any symbol in any object file was marked with the
__declspec(dllexport) attribute.

Althor
4th July 2010, 09:56
Hi

Now I undestand this behaviour. I thought that qMake was the responsible for saying to g++ what methods must be exported.

All is clear now.

Thanks a lot guys.
My best regards.