PDA

View Full Version : How to use methods from linked library written in Ansi C?



porterneon
5th March 2011, 23:33
Hello.
I have library that has been written in Ansi C named "edr.a" and header file "edr.h". Library hass been compiled on 32-bit ubuntu and is linked into project but I get undefined references to all methods defined in header file.
Please take a look on this simple example:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "edr.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

SetDebugMode(); // this is method defined in edr.h
}
edr.h file:

long SetDebugMode(void);
long ClearDebugMode(void);
This is what compilator is showing:


g++ -c -m32 -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib64/qt4/mkspecs/linux-g++-32 -I../Lib -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I../LinkowanieBiblioteki -I../Lib -I. -I. -I. -o moc_mainwindow.o moc_mainwindow.cpp
g++ -m32 -o LinkowanieBiblioteki main.o mainwindow.o moc_mainwindow.o -L -ledr -lQtGui -lQtCore -lpthread
mainwindow.o: In function `MainWindow':
/home/neon/workspace/qt4/LinkowanieBiblioteki-build-desktop/../LinkowanieBiblioteki/mainwindow.cpp:14: undefined reference to `SetDebugMode'
What I'm doing wrong? How to use linked external library written in Ansi C?

SixDegrees
6th March 2011, 01:20
You library isn't being linked; that's what an undefined reference means.

You'll have to add it to your project file, with something like 'LIBS += /path/to/my/library.a'

porterneon
6th March 2011, 08:41
In my .pro file there is "LIBS += -L -ledr". Even when I declare full path to library file "LIBS += /home/user/project/edr.a", there is the same issue.

This is what compile command:

g++ -m32 -o LinkowanieBiblioteki main.o mainwindow.o moc_mainwindow.o -L -ledr -lQtGui -lQtCore -lpthread

When I copied library edr.a into destination folder "/home/user/project-build-desktop" and run this command

g++ -m32 -o LinkowanieBiblioteki main.o mainwindow.o moc_mainwindow.o edr.a -lQtGui -lQtCore -lpthread

from console than it works. So it's looks like there is problem with path to library.

stampede
6th March 2011, 11:49
So it's looks like there is problem with path to library.
If you want dynamic linking, -L switch requires a path to directory where your library (.so on Linux or .dll on windows) is:

LIBS += -L"/home/user/project/" -ledr
If you want static linking (.a or .lib), do as SixDegrees written (without the -ledr switch).

porterneon
6th March 2011, 12:10
Hi all, thanx for reply.
I did static linking in simple project with MainWindow widget only and it works. Than I tryied to do the same in my project (now there is 7 form widgets). I had clined solution, rebuild everything and again undefined references. Everything I have made in the same way. What is wrong?

PS: I solved issue. extern "C" has fixed it.


extern "C"{
#include "edr.h"
}