PDA

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



g16bit
29th April 2011, 11:58
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

#ifndef MYLIB_H
#define MYLIB_H

class mylib
{
public:
//De/Construct function
mylib();
~mylib();
//Add function
inline int add(int a, int b) const;
};
#endif

file mylib.cpp

#include"mylib.h"
#include<QString>
#include<QDebug>

mylib::mylib() {}
mylib::~mylib() {}
inline int mylib::add(int a, int b) const
{
qDebug().operator <<("LCommon::Currently in int add(int, int) const function.");
return a+b;
}

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 += core gui
TARGET = use1
TEMPLATE = app
LIBS += $$(CPPLIBS)/libmylib.a
INCLUDEPATH += $$(CPPLIBS)/libinclude #contain header file here
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
and in the main function of the mainwindow.cpp file

int a = mylib::add(6,7);
and it give errors:

/mainwindow.cpp:13: error: undefined reference to `mylib::add(int, int) const'
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.

mcosta
29th April 2011, 13:07
Are you sure your code compiles? There are no errors?

The "add method" is a non static member of mylib.
Here


int a = mylib::add(6,7);

you use it as static member.

If you use inline you have to define (implement) in header file or in a file included directly

g16bit
29th April 2011, 17:52
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.

mcosta
29th April 2011, 18:06
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


CONFIG += shared


For more details read here and here

g16bit
30th April 2011, 06:13
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:

LIBS += $$(CPPLIBS)/static/mylib.a #for static lib
#LIBS += -lmylib #for using shared/dyn lib
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)

typedef int (*Myproto) (int, int);
QLibrary *lib = new QLibrary("mylib.so");
Myproto myf = (Myproto) lib->resolve("add");
if (myf)
{
qDebug().operator <<("Loaded");
} else {
qDebug().operator <<("NOT Loaded");
}
lib->unload();
delete lib;
when compiled this made no errors. But when I run prog the terminal export:

NOT Loaded
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.

DanH
30th April 2011, 18:15
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:
LIBS += -L<directory path for lib> -l<library name>
eg:
LIBS += -L/someDir/libDir -lmylib
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".