views:

9689

answers:

3

Hello,

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,

+1  A: 

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.

dirkgently
Hello, I have entered the code into the constructor. However, the program won't run because of the compile errors. Maybe its my library. The library was compiled with VS 2005.
robUK
+1  A: 

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.

Arnold Spence
Hello,I have tried both for dll and lib by adding the extension.LIBS += D:\Projects\qtDialer\tools\lib\UserAgentLib.libLIBS += D:\Projects\qtDialer\tools\lib\UserAgentLib.dllThe library was compiled with WIN32 visual studio 2005. Would that make any difference?Thanks for any advice
robUK
I don't use visual studio but you should use the full name of the library file that visual studio generated. I was only suggesting those extensions as possibilities.
Arnold Spence
One other thing you might try.. qmake really prefers forward slashes. If you have to use backslashes, double them up like this '\\'.
Arnold Spence
Hello. I think I know what the problem might be. I have tested with a library I have written in native visual C++ 2008. And linked it with a console app also created in visual C++ 2008. However, the library doesn't work for qt. I think I need to be compiling qt app in visual studio compiler.
robUK
I think I am compiling my apps in qt in g++ MinGW/QT. However, I am not sure how to change to VC++ as my library is compiled in that. I have been to the 'qt command prompt' And I can see on the first line. 'Setting up a MinGW/Qt only environment...' How can I change my compiler? Many thanks,
robUK
Are you using Qt Creator? Perhaps you could copy and paste some error text into the main question area. Anyway, if your library is built correctly and works from a VS project then it should work fine when compiling a project that uses it with MinGW. This is uncertain territory for me unfortunately.
Arnold Spence
Hello. Yes, I am using Qt Creator. I have cut and pasted into my question. I tested with a library I created myself with VS C++ 2008. This library works as I tested it with another VS application. However, it didn't work with QT. My error code is posted in my question. Many thanks.
robUK
I'm not the best at deciphering g++ errors but if I were to make a guess, maybe your library is linking with another library that g++ can't find? Does your skeleton app build and run without including your library (just a sanity check).
Arnold Spence
I did some googling and there was a reference to unicode strings as potentially being a problem. Maybe you can rebuild your library using VS but turning off the compiler switch for unicode or wide strings.
Arnold Spence
Yes, my qt app will run without the linking with the library. I have been making some changes. As soon as I put this line in: std::cout << "Add numbers: " << add_numbers(10, 20) << std::endl; I get 1 errors: collect2: ld returned 1 exit status. 1 warning: underfined reference to add_numbers(int,int)
robUK
However, I have included the header mathslibrary.hpp. so not sure why I would get undefined error. How about calling conventions? Could this be a problem? I also tried without unicode still the same problem. Many thanks,
robUK
I think it is finding the header ok but it's not finding the implementation of the function. You can test this by making a simple inline function implementation in the header file for 'add_numbers'.
Arnold Spence
I'll try to duplicate your project settings tonight and see what I find. I'll throw out another thing though, try to manually clean out your build directory and verify file permissions. I've had weird errors caused by these two things before :)
Arnold Spence
I've edited my answer. Using extern "C" should fix the simple test lib. It should work for you UserAgentLib too as long as it is export straight functions. Things break down when involving ojbects/classes when using different compilers. That's what COM is designed to deal with.
Arnold Spence
Thanks for your continued effort. I have added the extern "C". However, I am now getting these 3 errors. 1) error: undefined reference to `_RTC_Shutdown' 2) error: undefined reference to `_RTC_InitBase' 3) error: collect2: ld returned 1 exit status
robUK
I thing I am not sure about. I have created a static library in visual studio. In the directory of my qt project (where my qt *.cpp, *.pro, and makes files are).
robUK
I have a folder called tools and in that folder I have a folder called inc, which has my *hpp file. And another folder called libs, which has my static library. This is what I normally do. However is this directory structure ok for qt?
robUK
A new error,progress :) My knowledge on library usage and linking is fuzzy at best but static linking throws a complication. Now, I think you must explicitly link your program to any libraries that your static library. Use http://www.dependencywalker.com/ to find them.
Arnold Spence
I don't think there is a problem with your paths as long as the necessary paths are specified in your .pro file.
Arnold Spence
You could try building your library as a dynamic instead of a static library just for fun.
Arnold Spence
I think I have come to the end of the road on this problem. I am still working this problem. And when I get some positive results I will report back. Many thanks for all your past effort.
robUK
Ok, good luck. I did download VS express and built a test library. I followed a tutorial MS has, building a math library no less. I removed the class and namespace wrappers and used just extern "C" functions in a dynamic library (dll) and I was able to link to it ok from a Qt test project.
Arnold Spence
+1  A: 

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

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