PDA

View Full Version : Help needed linking to library



StephenR
2nd July 2009, 10:06
I have just started using Qt and would like to create an exe that calls functions in a dll. I have created a library as described here (http://doc.trolltech.com/4.5/sharedlibrary.html) and now I am trying to call an exported function from a Qt GUI application. When I try to build the exe I get the following error:

../TestLibrary/testfuncs.h:13: error: function `QString testfunc()' definition is marked dllimport.

I don't really understand this error because surely dllimport is what I want? Any advice would be appreciated.

wysota
2nd July 2009, 14:42
Did you export the symbols in the library correctly? When building the library the symbol should be declared as dllexport and when using it - as dllimport.

StephenR
2nd July 2009, 15:50
Did you export the symbols in the library correctly? When building the library the symbol should be declared as dllexport and when using it - as dllimport.

I have the following in my library:


#if defined(TESTLIB_LIBRARY)
#define TESTLIB_EXPORT Q_DECL_EXPORT
#else
#define TESTLIB_EXPORT Q_DECL_IMPORT
#endif

and


TESTLIB_EXPORT QString testfunc() { return "This is a test"; }

I have a DEFINES += TESTLIB_LIBRARY in the .pro file of the library. What is strange is if I comment out the conditional compilation so that TESTLIB_EXPORT is always defined as Q_DECL_EXPORT is seems to work. This doesn't make sense, because then surely it is not being specified as dllimport in the exe :confused:

wysota
2nd July 2009, 17:06
Your code is incorrect. testFunc() is defined in the header, it is part of the application that includes the header file so you can't dllimport it. Move the definition to the cpp file and it should work.

StephenR
3rd July 2009, 08:50
Your code is incorrect. testFunc() is defined in the header, it is part of the application that includes the header file so you can't dllimport it. Move the definition to the cpp file and it should work.

Thanks! That will teach me for trying to code it up quickly!