0

我正在研究在 QT 中链接共享库,尝试过教程但没有任何效果。有人可以帮我找出错误吗?

编译测试项目时出现错误“未定义的测试引用”。

我试图链接我的库的项目:loadTestLib.pro:

#-------------------------------------------------
#
# Project created by QtCreator 2013-04-22T15:32:30
#
#-------------------------------------------------

QT       += core

QT       -= gui

TARGET = loadTestLib
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp
LIBS+=-L../libtest.so.1.0.0

HEADERS += \
    test_global.h \
    test.h

主.cpp:

#include <QCoreApplication>
#include "test.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Test test;
    return a.exec();
}

test_global.h:

#ifndef TEST_GLOBAL_H
#define TEST_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(TEST_LIBRARY)
#  define TESTSHARED_EXPORT Q_DECL_EXPORT
#else
#  define TESTSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // TEST_GLOBAL_H

测试.h:

#ifndef TEST_H
#define TEST_H

#include "test_global.h"

class TESTSHARED_EXPORT Test
{
public:
    Test();
};

#endif // TEST_H

图书馆:

test_global.h:

#ifndef TEST_GLOBAL_H
#define TEST_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(TEST_LIBRARY)
#  define TESTSHARED_EXPORT Q_DECL_EXPORT
#else
#  define TESTSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // TEST_GLOBAL_H

测试.cpp:

#include "test.h"


Test::Test()
{
}

测试.h:

#ifndef TEST_H
#define TEST_H

#include "test_global.h"

class TESTSHARED_EXPORT Test
{
public:
    Test();
};

#endif // TEST_H

测试.pro:

#-------------------------------------------------
#
# Project created by QtCreator 2013-04-22T15:31:04
#
#-------------------------------------------------

QT       -= gui

TARGET = test
TEMPLATE = lib

DEFINES += TEST_LIBRARY

SOURCES += test.cpp

HEADERS += test.h\
        test_global.h

unix:!symbian {
    maemo5 {
        target.path = /opt/usr/lib
    } else {
        target.path = /usr/lib
    }
    INSTALLS += target
}

编译输出:

13:49:12: Running steps for project loadTestLib...
13:49:12: Configuration unchanged, skipping qmake step.
13:49:12: Starting: "/usr/bin/make" -w
make: Entering directory `/home/bonart/Workspace/test/loadTestLib-build-Desktop_Qt_5_0_1_GCC_64bit-_______'
/home/bonart/Qt5.0.1/5.0.1/gcc_64/bin/qmake -spec linux-g++-64 CONFIG+=debug CONFIG+=declarative_debug CONFIG+=qml_debug -o Makefile ../loadTestLib/loadTestLib.pro
make: Leaving directory `/home/bonart/Workspace/test/loadTestLib-build-Desktop_Qt_5_0_1_GCC_64bit-_______'
make: Entering directory `/home/bonart/Workspace/test/loadTestLib-build-Desktop_Qt_5_0_1_GCC_64bit-_______'
g++ -m64 -Wl,-rpath,/home/bonart/Qt5.0.1/5.0.1/gcc_64 -Wl,-rpath,/home/bonart/Qt5.0.1/5.0.1/gcc_64/lib -o loadTestLib main.o   -L./libs -L../libs/-ltest -L/home/bonart/Qt5.0.1/5.0.1/gcc_64/lib -lQt5Core -lpthread 
main.o: In function `main':
/home/bonart/Workspace/test/loadTestLib-build-Desktop_Qt_5_0_1_GCC_64bit-_______/../loadTestLib/main.cpp:7: undefined reference to `Test::Test()'
make: Leaving directory `/home/bonart/Workspace/test/loadTestLib-build-Desktop_Qt_5_0_1_GCC_64bit-_______'
collect2: ld returned 1 exit status
make: *** [loadTestLib] Error 1
13:49:12: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project loadTestLib (kit: Desktop Qt 5.0.1 GCC 64bit)
When executing step 'Сборка'

$ nm -D for my library 给了我:

w _Jv_RegisterClasses
                 U _Unwind_Resume
                 U _Z17qt_message_output9QtMsgTypeRK18QMessageLogContextRK7QString
                 U _ZN10QArrayData10deallocateEPS_mm
                 U _ZN11QTextStreamD1Ev
                 U _ZN11QTextStreamlsERK7QString
                 U _ZN11QTextStreamlsEc
0000000000000bb0 T _ZN4TestC1Ev
0000000000000bb0 T _ZN4TestC2Ev
0000000000000d90 W _ZN6QDebugD1Ev
0000000000000d90 W _ZN6QDebugD2Ev
                 U _ZN7QString15fromUtf8_helperEPKci
                 U _ZNK14QMessageLogger5debugEv
                 U _ZTISt9bad_alloc
                 U _ZdlPv
0000000000202080 A __bss_start
                 U __cxa_begin_catch
                 U __cxa_end_catch
                 w __cxa_finalize
                 w __gmon_start__
                 U __gxx_personality_v0
0000000000202080 A _edata
0000000000202090 A _end
0000000000000ec8 T _fini
00000000000009e8 T _init
4

3 回答 3

1

LIBS在 In loadTestLib.pro 中的行是错误的。它应该是

LIBS+=-L.. -ltest

或者,为了清楚起见,您可以将其放在两行(并使用空格)

LIBS += -L..
LIBS += -ltest

请注意,这将链接到 ../libtest.so。通常,您在构建时链接不带版本后缀的文件,并在运行时链接带有版本号的文件。

于 2013-04-22T19:27:13.797 回答
0

在 Windows 中,库的绝对路径和 .dll 的名称对我有用。在 Ubuntu 中,它也以相同的方式成功编译,但应用程序只有在将其放入 /usr/lib 时才能找到库

于 2013-04-24T17:04:25.363 回答
0

除了 Troubadour 所说的之外,您还应该告诉链接器它在哪里可以找到 lib。您可以在 .pro 文件中设置 QMAKE_LIBDIR,或者将包含您的库的目录添加到/etc/ld.so.conf并使用ldconfig命令。

于 2013-04-23T08:29:04.867 回答