[英]How an application on QT creator calls a static library compiled using gcc?
我是 QT 創作者的新手。 在 QT creator 上,我創建了一個靜態庫 (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);
static_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
小部件.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
小部件.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;
}
主程序
#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()
的名稱LibBFunc()
。
#include <stdio.h>
#ifdef __cplusplus
extern “C” {
#endif
int LibBFunc(int a, int b);
#ifdef __cplusplus
}
#endif
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.