PDA

View Full Version : Q_DECL_EXPORT and Q_DECL_IMPORT


Gopala Krishna
6th July 2007, 10:09
I had a look at the edyuk's svn link to qcumber posted by fullmetalcoder and found these (@fullmetalcoder: Hope you don't mind me posting this here :) )

#ifdef _QCUMBER_BUILD_
#if (defined(QT_DLL) || defined(QT_SHARED)) && !defined(QT_PLUGIN)
#define QCUMBER_EXPORT Q_DECL_EXPORT
#else
#define QCUMBER_EXPORT
#endif
#else
#define QCUMBER_EXPORT Q_DECL_IMPORT
#endif

I didn't understand this and tried searching in assistant for definition of Q_DECL_EXPORT and IMPORT but in vain. Then i looked at qglobal.h and found these macros defined to one of these based on some variable __declspec(dllexport),__attribute__((visibility("default"))),__declspec(dllimport)

Again i didn't understand. Googling didn't help much either since i don't even know the basics of this.
Can someone help me understand this clearly ?
And also can you tell me how this macro helps when used like this ?
class Q_DECL_EXPORT SomeClass : BaseClass
{
..
};
Finally (probably to fullmetalcoder)
Why is the QCUMBER_EXPORT defined to be Q_DECL_IMPORT ? (export - import :confused: )

jacek
6th July 2007, 11:41
Can someone help me understand this clearly ?
It's M$ idea. When you write DLLs, you have to explicitly mark objects (functions, classes or variables) that should be visible from the outside and you do this with __declspec(dllexport) tag. All other objects won't be accessible from the outside and only DLL code will be able to use them.

Using GCC and other Unix compilers you can do the same with linker scripts (or __attribute__ tag), so you don't need funny macros.

Why is the QCUMBER_EXPORT defined to be Q_DECL_IMPORT ? (export - import :confused: )
Because an object has to be exported when you compile the library and imported when you use the library. That's why you need a macro that is once Q_DECL_EXPORT and once Q_DECL_IMPORT.

daracne
6th July 2007, 11:49
__declspec(dllexport) and __declspec(dllimport) are both used for creating dynamic library modules. It tells the compiler to make a symbol so that the program calling the library is able to find and execute the function/class/ect. Defining import does exactly the opposite... it is used in the calling application and tells the compiler the code is external. Sorry if that doesn't make much sense/help out at the moment, 4am here =P. Try this article (series of 4), it does a decent job.

http://www.codeproject.com/dll/Dllfun.asp

The QCUMBER_EXPORT define serves as an ease of use feature. Instead of having to keep two seperate copies of the .h file or change between export/import, we can let the compiler decide which one to use. In the QCumber project you're wanting to export the code from, you'd define _QCUMBER_BUILD_ in the preprocessor flags. Then when its including by other projects, it'll automatically be set to import.

EDIT: Jacek beat me too it, and with clearly much more sleep ;-).

Gopala Krishna
7th July 2007, 15:22
Thanks for those nice overviews :)