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
Code:
#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
Code:
#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
Code:
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
Code:
int a = mylib::add(6,7);
and it give errors:
Code:
/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.
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
Code:
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
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.
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
For more details read here and here
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:
Code:
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)
Code:
typedef int (*Myproto) (int, int);
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:
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.
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:
Code:
LIBS += -L<directory path for lib> -l<library name>
eg:
Code:
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".