Results 1 to 5 of 5

Thread: create and use shared library in qt

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default create and use shared library in qt

    Hi everyone,
    I tried to create and use a shared library in qt.I used the c++ library project to create this.

    code used to create a shared library:

    .pro file:

    Qt Code:
    1. QT -= gui
    2.  
    3. TARGET = myLIB
    4. TEMPLATE = lib
    5.  
    6. DEFINES += MYLIB_LIBRARY
    7.  
    8. SOURCES += mylib.cpp
    9.  
    10. HEADERS += mylib.h\
    11. myLIB_global.h
    To copy to clipboard, switch view to plain text mode 

    .h file

    Qt Code:
    1. class MYLIBSHARED_EXPORT MyLIB {
    2. public:
    3. MyLIB();
    4. int add(int x,int y);
    5. };
    6.  
    7. extern "C"
    8. {
    9. int sub(int x,int y);
    10. }
    To copy to clipboard, switch view to plain text mode 

    .cpp file :

    Qt Code:
    1. MyLIB::MyLIB()
    2. {
    3. }
    4. int MyLIB::add(int x, int y)
    5. {
    6. return x+y;
    7. }
    8.  
    9. int sub(int x, int y)
    10. {
    11. return x-y;
    12. }
    To copy to clipboard, switch view to plain text mode 

    In my test application i am able to use sub function.code is
    Qt Code:
    1. typedef int (*pf)(int,int);
    2. pf fun=(pf)QLibrary::resolve("./libmyLIB.so","sub");
    3. int x=fun(20,10);
    To copy to clipboard, switch view to plain text mode 
    It is working.
    But when i am trying to use isLoaded function it is returning false.
    Qt Code:
    1. QLibrary myLib("./libmyLIB.so");
    2. ok=myLib.isLoaded();
    To copy to clipboard, switch view to plain text mode 


    My problem is i want to use the function directly without using QLibrary.(only adding in .pro file)
    How i will use add function in my application ?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: create and use shared library in qt

    See in the qmake documentation for LIBS, include the header file of your library and use your library classes as usual.

  3. #3
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: create and use shared library in qt

    thanks for reply.Can you tell me what should be written in .h file.One prototype or any thing extra.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: create and use shared library in qt

    In your case you probably only have to use
    Qt Code:
    1. #include "mylib.h"
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: create and use shared library in qt

    thanks it is working.

Similar Threads

  1. Replies: 2
    Last Post: 19th February 2011, 11:26
  2. How to link a shared library to app
    By kamlmish in forum Newbie
    Replies: 1
    Last Post: 25th December 2010, 20:57
  3. how to use shared library
    By assismvla in forum Qt Programming
    Replies: 3
    Last Post: 25th September 2009, 21:29
  4. Qt/Mac Shared Library
    By nareshqt in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2008, 07:21
  5. so shared library problem
    By mtrpoland in forum Newbie
    Replies: 3
    Last Post: 13th August 2007, 21:51

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
  •  
Qt is a trademark of The Qt Company.