Results 1 to 2 of 2

Thread: Creating and Using a Static Library (QT4.7.4, QT Creator 2.2.1, Red Hat Linux 6.1)

  1. #1
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy Creating and Using a Static Library (QT4.7.4, QT Creator 2.2.1, Red Hat Linux 6.1)

    I've been browsing the forums, and I've found a few posts that seem to address the same problem I'm having, but after implementing the solutions for those people, I still have problems.

    I am trying to create a static library with one .pro file, and then use that library in another .pro file.

    I created an example project to see if I could get it working. Here is the folder layout:

    Example
    --bin (Where the app and library build)
    --LibraryProject
    ----LibraryProject // (contains LibraryProject.pro and all the header/source files)
    ----LibraryProject-build-desktop
    --SampleProject
    ----SampleProject // (contains main.cpp hello world, would like to use the lib here)
    ----SampleProject-build-desktop

    This is my LibraryProject.pro file
    Qt Code:
    1. QT += core gui
    2.  
    3. TEMPLATE = lib
    4. CONFIG += staticlib
    5.  
    6. HEADERS += \
    7. SimpleClass.h
    8.  
    9. SOURCES += \
    10. SimpleClass.cpp
    11.  
    12. TARGET = MyLibrary
    13. DESTDIR = ../../bin/
    To copy to clipboard, switch view to plain text mode 

    This is the SimpleClass .h file (in the Library)
    Qt Code:
    1. #ifndef SIMPLECLASS_H
    2. #define SIMPLECLASS_H
    3.  
    4. class SimpleClass
    5. {
    6. public:
    7. SimpleClass();
    8. int ReturnNumber();
    9. };
    10.  
    11. #endif // SIMPLECLASS_H
    To copy to clipboard, switch view to plain text mode 

    This is the SimpleClass.cpp file (in the library)
    Qt Code:
    1. #include "simpleclass.h"
    2.  
    3. SimpleClass::SimpleClass()
    4. {
    5. }
    6.  
    7. int SimpleClass::ReturnNumber()
    8. {
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 

    This is the SampleProject.pro file
    Qt Code:
    1. LIBS += -L../../bin/ -lMyLibrary
    2.  
    3. INCLUDEPATH = ../../LibraryProject/LibraryProject
    4.  
    5. SOURCES += \
    6. main.cpp
    7.  
    8. DESTDIR += ../../bin
    To copy to clipboard, switch view to plain text mode 

    This is the main.cpp (In the Sample project)
    Qt Code:
    1. #include <iostream>
    2.  
    3. //How do I #include and use this library??
    4. //My #includes yield no .h's or libraries
    5. //that I made available to me... using
    6. //them anyways causes "not found" errors.
    7.  
    8. using namespace std;
    9.  
    10. int main()
    11. {
    12. //Would love to use the library right here
    13. //SampleClass...
    14. return 0;
    15. }
    To copy to clipboard, switch view to plain text mode 

    It seems no matter how I try to include the library...

    LIBS += -L../../bin/ -lMyLibrary
    INCLUDEPATH += ../../LibraryProject/LibraryProject/

    or...

    LIBS += ../../bin/libMyLibrary.a

    or

    The horrible Right-Click "Add Library" huge block of text...

    No matter what method I try, the library is not found in the main.cpp...

    What am I doing wrong?

    (Included on this post should be the very example I just described)

    (Crap, it's actually the UPDATEDExample.zip, not the Example.zip... but where the heck is the DELETE Attachment????)
    Attached Files Attached Files
    Last edited by bruceariggs; 27th August 2011 at 20:47.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Creating and Using a Static Library (QT4.7.4, QT Creator 2.2.1, Red Hat Linux 6.1

    Your updated example worked fine using Qt 4.7.4 & Qt Creator 2.2.1 on my Debian Sid system.
    Qt Code:
    1. #include <QDebug>
    2. #include "SimpleClass.h"
    3.  
    4. int main()
    5. {
    6. SimpleClass sc;
    7. qDebug() << sc.ReturnNumber();
    8. return 0;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Don't know anything about Red Hat but maybe this will help: link

  3. The following user says thank you to norobro for this useful post:

    bruceariggs (15th September 2011)

Similar Threads

  1. Replies: 2
    Last Post: 19th February 2011, 11:26
  2. Replies: 1
    Last Post: 18th February 2011, 11:08
  3. creating and using static library
    By indomie_seleraku in forum Newbie
    Replies: 3
    Last Post: 18th December 2010, 09:05
  4. Replies: 0
    Last Post: 4th November 2009, 07:45
  5. Creating Static library with QT
    By QPlace in forum Qt Programming
    Replies: 2
    Last Post: 21st December 2008, 04:50

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.