qt为第三方标题和库添加路径

时间:2009-03-15 12:02:01

标签: qt path include

我正在使用qt并开发一个可在win xp / vista下运行的桌面应用程序。

我有第三方库UserAgentLib(静态和共享)。但我不知道如何在qt创建者中链接。

我打开了* .pro文件并添加了我的库和标题路径。 该库名为UserAgentLib,头文件名为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

我认为它确实找到了头文件,因为我在UserAgentLib.h文件中获得了大约100个声明错误。但是,我不认为它与图书馆有联系。

非常感谢任何建议,

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

我在VS C ++ 2008中创建了一个非常简单的库。这是头文件和源文件的代码。 部首:

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

来源:

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

我把它编成了一个库。并通过链接VS 2008中的WIN32控制台应用程序进行测试。该库按预期工作。

现在,当我尝试与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();
}

这是我的qmake文件:

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

这些是我尝试构建时遇到的错误:

C:/ QT / mingw的/ bin中/../ LIB / GCC /的mingw32 / 3.4.5 /../../../ libmingw32.a(main.o),此:main.c中:: - 1 :错误:未定义引用`WinMain @ 16'

: - 1:错误:collect2:ld返回1退出状态

这些是编译问题:

运行项目testlibrary的构建步骤...

创建gdb宏库...

配置不变,跳过QMake步骤。

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

mingw32-make:进入目录`D:/ Projects / TestQTLibrary / testlibrary'

C:/ Qt / mingw / bin / mingw32-make -f Makefile.Debug mingw32-make [1]:进入目录`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]:离开目录`D:/ Projects / TestQTLibrary / testlibrary'

mingw32-make:离开目录`D:/ Projects / TestQTLibrary / testlibrary'

C:/ QT / mingw的/ bin中/../ LIB / GCC /的mingw32 / 3.4.5 /../../../ libmingw32.a(main.o),此:main.c中:

(。text + 0x104):未定义引用`WinMain @ 16' collect2:ld返回1退出状态 mingw32-make [1]: * [debug \ testlibrary.exe]错误1 mingw32-make:* [debug]错误2 退出代码2。 构建项目testlibrary时出错 执行构建步骤'Make'时

非常感谢任何建议,

4 个答案:

答案 0 :(得分:3)

不知道这是否会改变,但也许你必须这样定义:

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

答案 1 :(得分:1)

如果您遇到编译器错误,那么您的UserAgentLib.h可能没有包含在内。你可以用以下方法测试它:

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

您将上述内容放在.pro文件之一而不是构造函数中。请参阅this

如果图书馆没有链接(这是在你的应用程序编译好之后) - 那么你需要修改你的LIBS += ...行,虽然这看起来很好。

答案 2 :(得分:1)

首先尝试使用简单的库,然后尝试使用您实际尝试工作的库。

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

在.hpp文件中,在函数声明之前添加extern“C”:

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

从Visual Studio重建库。

现在您应该能够使用Qt Creater编译您的测试应用程序。然后将相应的dll复制到包含新可执行文件的目录中并运行它。

答案 3 :(得分:1)

据我所知,您使用MSVC生成了一个dll,现在您正尝试使用mingw在Qt中链接它。正确?

  

使用不同编译器创建的对象文件和静态库,或   即使有相同编译器的显着不同版本,也经常   不能联系在一起。这个问题不是MinGW特有的:很多   其他编译器是互不兼容的。从中构建一切   如果可以

,请使用相同版本的相同编译器

把这个拿出来:http://chadaustin.me/cppinterface.html

相关问题