QT创建者上的应用程序如何调用使用gcc编译的静态库



我是QT创建者的新手。在QTcreator上,我创建了一个静态库(libA.a(和一个调用该库(libA.a(的应用程序(AppA(,该库运行正常。使用gcc编译器,我创建了一个静态库(libB.a(和一个调用该库(libB.a(的应用程序(AppB(,该库也正常工作。

我希望QT创建者上的应用程序(AppA(调用库(libB.a(,但QT创建者给出了类似error: undefined reference to LibBFunc(int, int)的错误

有没有具体的电话方式?

此外,我的开发环境和代码如下。

  • Ubuntu 18.04 LTS

  • QT创建者4.8.2

  • gcc(Ubuntu 7.5.0-3ubuntu1~18.04(7.5.0

  • libB.a:static_lib_test.h,static_lib_test.c

  • AppA:AppWithLibrary.pro,widget.h,widget.cpp,main.cpp,widget.ui

static_lib_test.h

#include <stdio.h> 
int  LibBFunc(int a, int b);

静态lib_test.c

#include "static_lib_test.h" 
int  LibBFunc(int a, int b)
{
return a*b;
}

为库(libB.a(编译

gcc -c static_lib_test.c
ar rc libTest.a static_lib_test.o

AppWithLibrary.pro

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = AppWithLibrary
TEMPLATE = app
SOURCES += main.cpp
widget.cpp
HEADERS  += widget.h
FORMS    += widget.ui
LIBS += -L$$PWD/../build-MyUtil-Desktop_Qt_5_12_2_GCC_64bit-Debug/ -lMyUtil
LIBS += -L$$PWD/../Lib/ -lTest
INCLUDEPATH += $$PWD/../MyUtil
DEPENDPATH += $$PWD/../MyUtil
INCLUDEPATH += $$PWD/../Lib
DEPENDPATH += $$PWD/../Lib

widget.h

#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "myutil.h"
#include "static_lib_test.h" //정적 라이브러리 헤더파일
namespace Ui { class Widget; }
class Widget : public QWidget {
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private:
MyUtil *myUtil;
Ui::Widget *ui;
private slots:
void slotBtnClick();
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
//#include "static_lib_test.h" //정적 라이브러리 헤더파일
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
myUtil = new MyUtil();
connect(ui->pbtSum, SIGNAL(pressed()),
this,       SLOT(slotBtnClick()));
}
void Widget::slotBtnClick()
{
qint32 arg1 = ui->leArg1->text().toInt();
qint32 arg2 = ui->leArg2->text().toInt();
int a, b;
int c;
a=10;
b=20;
qint32 sumValue = myUtil->getSumValue(arg1, arg2);
c = LibBFunc(a, b);
ui->leSum->setText(QString("%1").arg(sumValue));
}
Widget::~Widget()
{
delete ui;
}

main.cpp

#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}

编译输出

../../../GccLib1/AppWithLibrary/widget.cpp: In member function ‘void Widget::slotBtnClick()’:
../../../GccLib1/AppWithLibrary/widget.cpp:21:9: warning: variable ‘c’ set but not used [-Wunused-but-set-variable]
int c;
^
g++ -Wl,-rpath,/home/pjs/Qt5.12.2/5.12.2/gcc_64/lib -o AppWithLibrary main.o widget.o moc_widget.o   -L/home/pjs/Projects/QT/GccLib1/AppWithLibrary/../build-MyUtil-Desktop_Qt_5_12_2_GCC_64bit-Debug/ -lMyUtil -L/home/pjs/Projects/QT/GccLib1/AppWithLibrary/../Lib/ -lTest -L/home/pjs/Qt5.12.2/5.12.2/gcc_64/lib -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread   
widget.o: In function `Widget::slotBtnClick()':
Makefile:263: recipe for target 'AppWithLibrary' failed
/home/pjs/Projects/QT/13_Library/01_libExample/build-AppWithLibrary-Desktop_Qt_5_12_2_GCC_64bit-Debug/../../../GccLib1/AppWithLibrary/widget.cpp:26: undefined reference to `LibBFunc(int, int)'
collect2: error: ld returned 1 exit status
make: *** [AppWithLibrary] Error 1
00:42:51: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project AppWithLibrary (kit: Desktop Qt 5.12.2 GCC 64bit)
When executing step "Make"
00:42:52: Elapsed time: 00:04.

您使用C编译器构建了libTest.a,并在C++应用程序上使用它。您需要static_lib_test.h中的extern "C" { ... },如下所示,以使C++编译器(如g++(关闭LibBFunc()的名称篡改。

#include <stdio.h> 
#ifdef __cplusplus
extern “C” {
#endif
int  LibBFunc(int a, int b);
#ifdef __cplusplus
}
#endif

最新更新