PDA

View Full Version : Compilation error when using static ibrary .a file created with QT in Console APP



sangameshwar
11th October 2013, 10:43
I am using QT Creator 2.4.1 on WindoI w7 with MinGW compiler.
1) Created MyLib.a using static library template provided by QT IDE.
2) Create MyApp.exe console application with QT.
3) Did below setting in MyApp.pro file to link the library
INCLUDEPATH +=../MyQTLib
LIBS+= -L"../MyQTLib-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug/debug/libMyQTLib.a"

4) Created object of the class defined in library and called the function.
5) While compiling MyApp.pro i am getting below error:

D:\QTSamples\MyQTApp-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug/../MyQTApp/main.cpp:10: undefined reference to `MyQTLib::MyQTLib()'
D:\QTSamples\MyQTApp-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug/../MyQTApp/main.cpp:12: undefined reference to `MyQTLib::getNumber()'
collect2: ld returned 1 exit status
mingw32-make.exe[1]: *** [debug\MyQTApp.exe] Error
6) I followed steps in http://qt-project.org/wiki/How_to_create_a_library_with_Qt_and_use_it_in_an_a pplication still getting the error.

Details :
////////////////////////////////////////myqtlib.cpp////////////////////////////////
#include "myqtlib.h"
MyQTLib::MyQTLib()
{
}
int MyQTLib::getNumber()
{
return 100;
}


//////////////////////////main.cpp///////////////////////////////
#include <QtCore/QCoreApplication>
#include <iostream>
#include <myqtlib.h>

using namespace std;

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyQTLib b;

cout << "Hello World" << "number" << b.getNumber()<<endl;
return a.exec();
}

////////////////MyQTLib.pro/////////////////////////////
QT -= core gui

TARGET = MyQTLib
TEMPLATE = lib
CONFIG += staticlib

SOURCES += myqtlib.cpp

HEADERS += myqtlib.h
unix:!symbian {
maemo5 {
target.path = /opt/usr/lib
} else {
target.path = /usr/lib
}
INSTALLS += target
}
//////////////////////MyQtApp.pro////////////////////

QT += core

QT -= gui

TARGET = MyQTApp
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app
INCLUDEPATH +=../MyQTLib
LIBS+= -L"../MyQTLib-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug/debug/libMyQTLib.a"
SOURCES += main.cpp
//////////////////////////////////////////

Please help ..

myta212
11th October 2013, 15:20
Hi,
Are you using windows ?
if yes,
please change this line :

LIBS+= -L"../MyQTLib-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug/debug/libMyQTLib.a"
to

LIBS+= -L"../MyQTLib-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug/debug/" -lMyQTLib

Best regards,

Toto

sangameshwar
14th October 2013, 10:59
Thanks Toto , it did solve the compilation issue mentioned below.

However i am facing some more compilation errors while using sbml library .
I have build libSBML library in Windows 7 machine with MinGW compiler.
However while using the built library inside my program (console application) i am getting linking errors.

I have build libSBML statically using below commands:
1) export CXXFLAGS="-DLIBSBML_STATIC -DLIBLAX_STATIC -I ./dependencies/include
/xercesc”

2) ./configure --enable-static --with-xerces=./dependencies --prefix="/home/LABS
9/outputLibSbml/"

3) make LDFLAGS=-no-undefined
//////////////////////////////////main.cpp//////////////////////////
#include <sbml/SBMLTypes.h>
#include <sbml/common/extern.h>
int main(int argc, char *argv[])
{
SBMLDocument* document;
SBMLReader reader;
return 0;
}
///////////////////////////////////////
/////////////////////error ////////////////////////////////////////////
debug/main.o: In function `main':
D:\QTSamples\TestSBML-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug/../TestSBML/main.cpp:20: undefined reference to `_imp___ZN10SBMLReaderC1Ev'D:\QTSamples\TestSBML-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug/../TestSBML/main.cpp:22: undefined reference to `_imp___ZN10SBMLReaderD1Ev'D:\QTSamples\TestSBML-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug/../TestSBML/main.cpp:22: undefined reference to `_imp___ZN10SBMLReaderD1Ev'collect2: ld returned 1 exit status
////////////////////////////////////////////////////////////////////////////////////

//////////////////TestSBML.pro/////////////////////////////
TARGET = TestSBML
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

INCLUDEPATH +=../sbmlAll
LIBS+= -L"../sbmlAll/sbmllib" -lsbml
/////////////////////////////////////////////////////////////////////////////////////

Please let me know if any idea.
Thanks.


Hi,
Are you using windows ?
if yes,
please change this line :

LIBS+= -L"../MyQTLib-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug/debug/libMyQTLib.a"
to

LIBS+= -L"../MyQTLib-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug/debug/" -lMyQTLib

Best regards,

Toto

ChrisW67
14th October 2013, 21:20
That is a linker error. Linker errors are often the result of your LIBS variable being wrong. Either the path associated with the -L option or the name of the library is wrong.