Results 1 to 9 of 9

Thread: Using content from a dynamic library (Windows)

  1. #1
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Using content from a dynamic library (Windows)

    Hi All

    I'm developing my own logger project.I wish it to be an external .dll file added to other project,without need of compilation or somesuch.
    I've created some test project,added logger.lib to linker libs,copied logger.dll into my test project-but it does not work when I try to use one of my macros :/
    What else shoud be done to make it work?To use QLibrary::resolve()?

    Below my header files:
    Qt Code:
    1. #ifndef LOGGER_GLOBAL_H
    2. #define LOGGER_GLOBAL_H
    3.  
    4. #include <Qt/qglobal.h>
    5.  
    6. #define maxLevel 4
    7.  
    8. #ifdef LOGGER_LIB
    9. # define LOGGER_EXPORT Q_DECL_EXPORT
    10. #else
    11. # define LOGGER_EXPORT Q_DECL_IMPORT
    12. #endif
    13.  
    14. #define LOGDEBUG_Logger(text,loggerId,level) Logger::printLog(text,loggerId,__LINE__,__FILE__,level);
    15. #define LOGDEBUG_Level(text,level) LOGDEBUG_LOGGER(text,"default",level)
    16. #define LOGDEBUG(text) LOGDEBUG_Logger(text,"default",maxLevel)
    17.  
    18. #endif // LOGGER_GLOBAL_H
    To copy to clipboard, switch view to plain text mode 

    and logger.h file:
    Qt Code:
    1. #ifndef LOGGER_H
    2. #define LOGGER_H
    3.  
    4. #include <QMap>
    5. #include <QObject>
    6. #include "logger_global.h"
    7. #include "logwindow.h"
    8.  
    9. LOGGER_EXPORT enum LogMode
    10. {
    11. None=0,
    12. Window,
    13. File,
    14. WindowAndFile
    15. };
    16.  
    17. class LoggerObject;
    18. class LOGGER_EXPORT Logger : public QObject
    19. {
    20. QMap<QString,LoggerObject*> loggers;
    21. static Logger *instance;
    22. LoggerObject *defaultLogger;//for convience and to avoid getting it from the map all the time
    23.  
    24. static inline bool createManager(void);
    25. Logger(QObject *parent=0);
    26.  
    27. public:
    28. static LoggerObject* getLoggerObject(const QString &id);
    29. static void printLog(const QString &text,const QString &loggerID,int line=0,const QString &file=QString(),char level=maxLevel,char type=0);
    30.  
    31. virtual ~Logger(void);
    32. };
    33.  
    34. class LOGGER_EXPORT LoggerObject : public QObject
    35. {
    36. friend class Logger;
    37.  
    38. QList<LogData> logs;//contains log messages.Filled by Logger::printLog()
    39.  
    40. QString id;//identifier of logger in Logger::loggers map
    41. LogMode mode;//determines where the logs shoud be put in
    42. QString logFile;//name and path of the log file
    43. LogWindow *logWindow;
    44.  
    45. void printLog(const QString &text,int line=0,const QString &file=QString(),char level=maxLevel,char type=0);
    46. LoggerObject(const QString &id,QObject *parent)
    47. :QObject(parent),id(id),logFile(id+".log.xml"),mode(Window)
    48. {};
    49.  
    50. public:
    51. QString identifier(void);
    52. void setLogMode(LogMode mode);
    53. LogMode logMode(void);
    54. void setLogFile(const QString &filename);
    55. QString logFileName(void);
    56.  
    57. //co jeszcze trzeba
    58. };
    59. #endif // LOGGER_H
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2011
    Location
    Seoul, Korea
    Posts
    46
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using content from a dynamic library (Windows)

    Hi there,

    can you specify more about your problem? when you say "it doesn't work", what do you mean by it? compile error (if so what error message)? runtime error?

    Regards,
    Last edited by Lykurg; 9th August 2011 at 09:48. Reason: Removed full quote
    Dong Back Kim

  3. #3
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Using content from a dynamic library (Windows)

    Sorry,I forgot to add compilation errors:
    loggerdlltest.cpp(7) : error C2653: 'Logger' : is not a class or namespace name
    loggerdlltest.cpp(7) : error C3861: 'printLog': identifier not found, even with argument-dependent lookup

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using content from a dynamic library (Windows)

    You need to #include "logger.h" in your program.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Using content from a dynamic library (Windows)

    Mhm...so even having a *.dll and appropriate *.lib file I have to provide all needed header files,right?Binary code shoud be inside .dll I assume,and if so there shoud be no need for .cpp's

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using content from a dynamic library (Windows)

    There is no need for cpp files but you need to provide the headers. I suggest you change your macros to functions. Then you'll only have to provide headers with prototypes of those functions and the implementation that uses the Logger class (together with the header for that class) can remain hidden (and then you don't have to expose the class using LOGGER_EXPORT).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    MasterBLB (9th August 2011)

  8. #7
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Using content from a dynamic library (Windows)

    Quote Originally Posted by wysota View Post
    I suggest you change your macros to functions. Then you'll only have to provide headers with prototypes of those functions and the implementation that uses the Logger class (together with the header for that class) can remain hidden (and then you don't have to expose the class using LOGGER_EXPORT).
    I'd wish to do it that way,but I can't :/ all because of that __LINE__ and __FILE__,it has to be placed at the line of using a macro.
    Hmmmm...or there is a solution,instead of
    Qt Code:
    1. #define LOGDEBUG_Logger(text,loggerId,level) Logger::printLog(text,loggerId,__LINE__,__FILE__,level);
    To copy to clipboard, switch view to plain text mode 
    use something like:
    Qt Code:
    1. void someGlobalFunction(params);//declaration there,definition inside let's say logger.cpp and therefore inside the .dll file
    2. #define LOGDEBUG_Logger(text,loggerId,level) someGlobalFunction(text,loggerId,__LINE__,__FILE__,level);
    To copy to clipboard, switch view to plain text mode 
    and inside it I shoud invoke my Logger::printLog


    Added after 14 minutes:


    Well,thanks Wysota,that idea of using a global function instead of Logger::printLog helped to hide all the complexity.But now I have an issue with linker:
    loggerdlltest.obj : error LNK2019: unresolved external symbol _logPrint referenced in function "public: __thiscall LoggerDLLTest::LoggerDLLTest(class QWidget *,class QFlags<enum Qt::WindowType>)" (??0LoggerDLLTest@@QAE@PAVQWidget@@V?$QFlags@W4Win dowType@Qt@@@@@Z)
    C:\Program Files\Microsoft Visual Studio .NET 2003\Projects\LoggerDLLTest\Release\LoggerDLLTest. exe : fatal error LNK1120: 1 unresolved externals

    How to make it work?I changed my file logger_global.h to:
    Qt Code:
    1. extern "C" void __cdecl logPrint(const QString &text,const QString &loggerID,int line=0,const QString &file=QString(),char level=maxLevel,char type=0);
    2.  
    3. #define LOGDEBUG_Logger(text,loggerId,level) logPrint(text,loggerId,__LINE__,__FILE__,level);
    To copy to clipboard, switch view to plain text mode 
    Last edited by MasterBLB; 9th August 2011 at 14:06.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using content from a dynamic library (Windows)

    Export the logPrint function (using LOGGER_EXPORT or whatever).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    MasterBLB (9th August 2011)

  11. #9
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Using content from a dynamic library (Windows)

    HELLL YEAH!All works!
    Big thanks to you Wysota

Similar Threads

  1. Dynamic library on Mac, Library not loaded
    By grayfox in forum Newbie
    Replies: 2
    Last Post: 2nd July 2011, 02:42
  2. Dynamic Scrollarea Content
    By rouge in forum Qt Programming
    Replies: 6
    Last Post: 14th May 2011, 08:37
  3. Dynamic library with GUI for Mac
    By mouse_sonya in forum Qt Programming
    Replies: 1
    Last Post: 26th July 2010, 12:23
  4. Linking Qt in a dynamic library
    By dave_mm0 in forum Qt Programming
    Replies: 4
    Last Post: 18th July 2009, 16:28
  5. How to use a Dynamic Link Library with QT / C++.
    By nivaldonicolau in forum Newbie
    Replies: 5
    Last Post: 29th April 2009, 14:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.