ant2009
ant2009

Reputation: 22486

qt add path for 3rd party header and libraries

I am using qt and developing a desktop app that will run under win xp/vista.

I have a 3rd party library UserAgentLib (static, and shared). But I am not sure how to link in qt creator.

I have opened the *.pro file and added my library and header path. The library is called UserAgentLib and the header file is called UserAgentLib.h

TARGET = Dialer
TEMPLATE = app

LIBS += D:\Projects\qtDialer\tools\lib\UserAgentLib
INCLUDEPATH += D:\Projects\qtDialer\tools\inc

SOURCES += main.cpp\
        catdialer.cpp

HEADERS  += catdialer.h

FORMS    += catdialer.ui

I think it does find the header file, as I get about 100 errors for declarations in the UserAgentLib.h file. However, I don't think it is linking with the library.

Many thanks for any suggestions,

======================

I have create a very simple library in VS C++ 2008. Here is the code for the header and source file. Header:

// mathslibrary.hpp
int add_numbers(const int a, const int b);

Source:

// mathslibrary.cpp
#include "mathslibrary.hpp"
int add_numbers(const int a, const int b)
{
return a + b;
}

I have compiled this into a library. And tested by linking with a WIN32 console application in VS 2008. The library worked as expected.

Now when I try and link with qt.

#include <QtCore/QCoreApplication>
#include <iostream>
#include "mathslibrary.hpp"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    std::cout << "add numbers 40 + 60 = " << add_numbers(40, 60) << std::endl;
    return a.exec();
}

This is my qmake file:

QT       -= gui
TARGET = testlibrary
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
LIBS = D:\Projects\TestLibrary\mathsLibrary\Debug\mathsLibrary.lib
INCLUDEPATH = D:\Projects\TestLibrary\mathsLibrary\
SOURCES += main.cpp

These are the errors I get when I try and build:

c:/Qt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../libmingw32.a(main.o):main.c::-1: error: undefined reference to `WinMain@16'

:-1: error: collect2: ld returned 1 exit status

And these are the compile issues:

Running build steps for project testlibrary...

Creating gdb macros library...

Configuration unchanged, skipping QMake step.

Starting: C:/Qt/mingw/bin/mingw32-make.exe debug -w

mingw32-make: Entering directory `D:/Projects/TestQTLibrary/testlibrary'

C:/Qt/mingw/bin/mingw32-make -f Makefile.Debug mingw32-make[1]: Entering directory `D:/Projects/TestQTLibrary/testlibrary'

g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-subsystem,console -mthreads -Wl -o debug\testlibrary.exe -L"c:\Qt\qt\lib"

D:\Projects\TestLibrary\mathsLibrary\Debug\mathsLibrary.lib -lQtCored4 mingw32-make[1]: Leaving directory `D:/Projects/TestQTLibrary/testlibrary'

mingw32-make: Leaving directory `D:/Projects/TestQTLibrary/testlibrary'

c:/Qt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../libmingw32.a(main.o):main.c:

(.text+0x104): undefined reference to `WinMain@16' collect2: ld returned 1 exit status mingw32-make[1]: * [debug\testlibrary.exe] Error 1 mingw32-make: * [debug] Error 2 Exited with code 2. Error while building project testlibrary When executing build step 'Make'

Many thanks for any advice,

Upvotes: 0

Views: 36389

Answers (4)

Kiran P
Kiran P

Reputation: 730

As far as i understood, you generated a dll using MSVC and now you are trying to link it in Qt using mingw. right?

Object files and static libraries created with different compilers, or even with significantly different releases of the same compiler, often cannot be linked together. This issue is not specific to MinGW: many other compilers are mutually incompatible. Build everything from source with the same version of the same compiler if you can

chech this out : http://chadaustin.me/cppinterface.html

Upvotes: 1

corn&#233;
corn&#233;

Reputation: 798

Don't know if this changes anything, but maybe you have to define it like this:

LIBS += -LD:/Projects/qtDialer/tools/lib -lUserAgentLib

Upvotes: 3

Arnold Spence
Arnold Spence

Reputation: 22272

Try this with the simple library first and then try it with the library you are actually trying to get working.

 LIBS += D:\Projects\qtDialer\tools\lib\mathsLibrary.lib

In your .hpp file, add extern "C" before your function declarations:

// mathslibrary.hpp
extern "C" int add_numbers(const int a, const int b);

Rebuild the library from Visual Studio.

Now you should be able to compile your test app with Qt Creater. Then copy the corresponding dll into the directory with your new executable and give it a run.

Upvotes: 1

dirkgently
dirkgently

Reputation: 111120

If you are getting compiler errors then your UserAgentLib.h probably didn't get included. You can test it with:

!exists( UserAgentLib.h ) {
 error( "No UserAgentLib.h file found" )
}

You put the above in one of the .pro file and not the constructor.See this.

If the library didn't get linked (which is after your application has compiled well) -- then you need to tinker with your LIBS += ... line, though which appears fine on first glance.

Upvotes: 1

Related Questions