簡體   English   中英

在自己的Qt應用程序中使用自己的靜態庫

[英]Using your own static library in your own Qt application

遵循如何使用Qt創建庫並在應用程序中使用它之后 ,我能夠在自己的Qt應用程序中創建和使用自己的共享庫。

在本教程中,我繼續學習“ 創建靜態庫”這一部分,我可能(也可能不會)成功理解。 我能夠構建libtest.a但是在嘗試使用它時,我得到了:

15:35:25: Running steps for project loadTestLib...
15:35:25: Starting: "C:\Qt\Qt5.5.1\5.5\mingw492_32\bin\qmake.exe" C:\Users\User\Downloads\library\client\static\loadTestLib.pro -r -spec win32-g++
15:35:27: The process "C:\Qt\Qt5.5.1\5.5\mingw492_32\bin\qmake.exe" exited normally.
15:35:27: Starting: "C:\Qt\Qt5.5.1\Tools\mingw492_32\bin\mingw32-make.exe" 
C:/Qt/Qt5.5.1/Tools/mingw492_32/bin/mingw32-make -f Makefile.Release
mingw32-make[1]: Entering directory 'C:/Users/User/Downloads/library/client/static'
g++ -c -pipe -fno-keep-inline-dllexport -O2 -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I. -I. -I.. -I..\testlib -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtWidgets -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtGui -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtANGLE -IC:\Qt\Qt5.5.1\5.5\mingw492_32\include\QtCore -Irelease -IC:\Qt\Qt5.5.1\5.5\mingw492_32\mkspecs\win32-g++  -o release\main.o ..\main.cpp
g++ -Wl,-s -Wl,-subsystem,windows -mthreads -o release\client.exe release/main.o  -lmingw32 -LC:/Qt/Qt5.5.1/5.5/mingw492_32/lib -lqtmain -lshell32 -L../testlib/static/release -ltest -lQt5Widgets -lQt5Gui -lQt5Core 
release/main.o:main.cpp:(.text+0x2d): undefined reference to `_imp___ZN6WidgetC1Ev'
Makefile.Release:77: recipe for target 'release\client.exe' failed
mingw32-make[1]: Leaving directory 'C:/Users/User/Downloads/library/client/static'
release/main.o:main.cpp:(.text+0x62): undefined reference to `_imp___ZTV6Widget'
release/main.o:main.cpp:(.text+0xa6): undefined reference to `_imp___ZTV6Widget'
C:/Qt/Qt5.5.1/Tools/mingw492_32/bin/../lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/bin/ld.exe: release/main.o: bad reloc address 0x13 in section `.eh_frame'
C:/Qt/Qt5.5.1/Tools/mingw492_32/bin/../lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [release\client.exe] Error 1
makefile:34: recipe for target 'release' failed
mingw32-make: *** [release] Error 2
15:35:32: The process "C:\Qt\Qt5.5.1\Tools\mingw492_32\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project loadTestLib (kit: Desktop)
When executing step "Make"
15:35:32: Elapsed time: 00:06.

這是該庫的代碼:

test.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TEMPLATE = lib
CONFIG += staticlib

CONFIG(debug, debug|release) {
    DESTDIR = debug
} else {
    DESTDIR = release
}

OBJECTS_DIR = $$DESTDIR
MOC_DIR = $$DESTDIR
RCC_DIR = $$DESTDIR
UI_DIR = $$DESTDIR

DEPENDPATH += . ..

INCLUDEPATH += . ..

SOURCES += ../test.cpp

HEADERS += ../test.h

DEFINES += TEST

test.h

#include <QtGui>

#if defined TEST
#define TEST_COMMON_DLLSPEC Q_DECL_EXPORT
#else
#define TEST_COMMON_DLLSPEC Q_DECL_IMPORT
#endif

#include <QWidget>

class TEST_COMMON_DLLSPEC Widget : public QWidget
{
    Q_OBJECT
public:
    Widget();
};

TEST.CPP

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

Widget::Widget() : QWidget() {}

這是客戶端應用程序的代碼:

loadTestLib.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = client

TEMPLATE = app

CONFIG(debug, debug|release) {
    DESTDIR = debug
} else {
    DESTDIR = release
}

OBJECTS_DIR = $$DESTDIR
MOC_DIR = $$DESTDIR
RCC_DIR = $$DESTDIR
UI_DIR = $$DESTDIR

SOURCES += ../main.cpp

DEPENDPATH += . ../testlib

INCLUDEPATH += . .. ../testlib

LIBS += -L../testlib/static/release -ltest

main.cpp中

#include <QApplication>

#include "testlib/test.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Widget w;
    w.resize(200,200);
    w.show();

    return a.exec();
}

我想念的是什么?

正如您對問題的評論中已經說過的那樣,動態鏈接所需的宏在靜態鏈接中引起麻煩,因為在靜態鏈接時不應定義它們。

如果宏在qcompilerdetection.h中定義為某項或為空,則取決於您的環境和/或編譯器。

例如,在Windows環境中,它們的定義如下:

#define Q_DECL_EXPORT __declspec(dllexport)
#define Q_DECL_IMPORT __declspec(dllimport)

如果要使代碼與多個環境兼容,可以執行以下操作:

在頭文件中:

#ifndef TEST_STATIC
#if defined TEST
#define TEST_COMMON_DLLSPEC Q_DECL_EXPORT
#else
#define TEST_COMMON_DLLSPEC Q_DECL_IMPORT
#endif
#else
#define TEST_COMMON_DLLSPEC
#endif


class TEST_COMMON_DLLSPEC Widget : public QWidget
{
};

靜態dll專業版和與靜態dll鏈接的應用程序專業版:

DEFINES += TEST_STATIC
  • TEST_COMMON_DLLSPEC擴展為空

動態dll專業版:

DEFINES += TEST
  • TEST_COMMON_DLLSPEC擴展到Q_DECL_EXPORT

應用程序與動態dll鏈接:

  • TEST_COMMON_DLLSPEC擴展到Q_DECL_IMPORT

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM