Results 1 to 6 of 6

Thread: Using static library? <I've search&read in this forum but wasnt solved. So please!>

  1. #1
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using static library? <I've search&read in this forum but wasnt solved. So please

    My purpose is to create and use a very simple static library. I have successful in building library (both .a and .so). The problem is I dont know how to use a function from my lib just like the way QString do: QString::number(2); for example. So I post my code here and the ways I've tried:

    mylib.h
    Qt Code:
    1. #ifndef MYLIB_H
    2. #define MYLIB_H
    3.  
    4. class mylib
    5. {
    6. public:
    7. //De/Construct function
    8. mylib();
    9. ~mylib();
    10. //Add function
    11. inline int add(int a, int b) const;
    12. };
    13. #endif
    To copy to clipboard, switch view to plain text mode 

    file mylib.cpp
    Qt Code:
    1. #include"mylib.h"
    2. #include<QString>
    3. #include<QDebug>
    4.  
    5. mylib::mylib() {}
    6. mylib::~mylib() {}
    7. inline int mylib::add(int a, int b) const
    8. {
    9. qDebug().operator <<("LCommon::Currently in int add(int, int) const function.");
    10. return a+b;
    11. }
    To copy to clipboard, switch view to plain text mode 
    from 2 files above I compiled and got file libmylib.a
    Now I want to use add function so I make a simple project like this:
    file use1.pro
    Qt Code:
    1. QT += core gui
    2. TARGET = use1
    3. TEMPLATE = app
    4. LIBS += $$(CPPLIBS)/libmylib.a
    5. INCLUDEPATH += $$(CPPLIBS)/libinclude #contain header file here
    6. SOURCES += main.cpp\
    7. mainwindow.cpp
    8. HEADERS += mainwindow.h
    To copy to clipboard, switch view to plain text mode 
    and in the main function of the mainwindow.cpp file
    Qt Code:
    1. int a = mylib::add(6,7);
    To copy to clipboard, switch view to plain text mode 
    and it give errors:
    Qt Code:
    1. /mainwindow.cpp:13: error: undefined reference to `mylib::add(int, int) const'
    To copy to clipboard, switch view to plain text mode 
    I've find about 5-6 post in this forum about using library in Qt and still dont get any solution for this. Im a newbie here so please help me. Thanks alot.


    Added after 7 minutes:


    In Addition, I've already include mylib.h file in mainwindow.h.
    Last edited by g16bit; 29th April 2011 at 11:58.

  2. The following user says thank you to g16bit for this useful post:

    DanH (30th April 2011)

  3. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using static library? <I've search&read in this forum but wasnt solved. So please

    Are you sure your code compiles? There are no errors?

    The "add method" is a non static member of mylib.
    Here
    Qt Code:
    1. int a = mylib::add(6,7);
    To copy to clipboard, switch view to plain text mode 
    you use it as static member.

    If you use inline you have to define (implement) in header file or in a file included directly
    A camel can go 14 days without drink,
    I can't!!!

  4. The following user says thank you to mcosta for this useful post:

    g16bit (29th April 2011)

  5. #3
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using static library? <I've search&read in this forum but wasnt solved. So please

    Thank you for your support. I have fixed my code and it worked. I need to fix my C++ skill, too. And your guide help me a lot. Now I can use mylib as a static lib. I thought about using it as a shared lib. But I had no idea how to implement it. Now I'm reading help file about QLibrary. I will try to code & make something before asking you about this. Thank you for your help.

  6. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using static library? <I've search&read in this forum but wasnt solved. So please

    QLibrary is for "runtime loaded" libraries.

    If you only need to use a "shared" library, you have only to modify mylib.pro and add the line
    Qt Code:
    1. CONFIG += shared
    To copy to clipboard, switch view to plain text mode 

    For more details read here and here
    A camel can go 14 days without drink,
    I can't!!!

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

    g16bit (29th April 2011)

  8. #5
    Join Date
    Mar 2011
    Posts
    8
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using static library? <I've search&read in this forum but wasnt solved. So please

    Sorry because my network connection had problems last night so I cant reply immediately. I've done as your guide and It's OK now. I can use both static & shared lib. The only dif between the 2 is:
    Qt Code:
    1. LIBS += $$(CPPLIBS)/static/mylib.a #for static lib
    2. #LIBS += -lmylib #for using shared/dyn lib
    To copy to clipboard, switch view to plain text mode 
    The runtime-loaded lib may take me sometime to learn.
    I wonder may runtime-loaded lib used widely in real world?


    Added after 1 51 minutes:


    I was tried to learn how to use QLibrary and I got my code like this: (I got mylib.so and try to load function "add" in this lib with QLibrary)
    Qt Code:
    1. typedef int (*Myproto) (int, int);
    2. QLibrary *lib = new QLibrary("mylib.so");
    3. Myproto myf = (Myproto) lib->resolve("add");
    4. if (myf)
    5. {
    6. qDebug().operator <<("Loaded");
    7. } else {
    8. qDebug().operator <<("NOT Loaded");
    9. }
    10. lib->unload();
    11. delete lib;
    To copy to clipboard, switch view to plain text mode 
    when compiled this made no errors. But when I run prog the terminal export:
    Qt Code:
    1. NOT Loaded
    To copy to clipboard, switch view to plain text mode 
    So, the function didnot load successful. Trying to find the reason. If this code has any errors, please tell me. thanks.

    The library has no change with the code has worked with static/shared before.
    Last edited by g16bit; 30th April 2011 at 06:12.

  9. #6
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Using static library? <I've search&read in this forum but wasnt solved. So please

    There's some variation in how you do it from platform to platform (and you haven't specified which platform you're using), but generally you specify a library in the .pro file by:
    Qt Code:
    1. LIBS += -L<directory path for lib> -l<library name>
    To copy to clipboard, switch view to plain text mode 
    eg:
    Qt Code:
    1. LIBS += -L/someDir/libDir -lmylib
    To copy to clipboard, switch view to plain text mode 
    If the lib is a DLL you put ".lib" on the end of the lib name, but if it's a static lib you leave it "bare".

Similar Threads

  1. Replies: 2
    Last Post: 19th February 2011, 11:26
  2. Replies: 7
    Last Post: 22nd December 2010, 08:13
  3. Memory could not be read? (runtime) [solved]
    By DrDonut in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2009, 10:15
  4. MinGW Windows Library Search Path
    By zztop in forum Qt Programming
    Replies: 4
    Last Post: 26th March 2006, 15:18

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.