简体   繁体   中英

How an application on QT creator calls a static library compiled using gcc?

I am a newbie for QT creator. On QT creator, I made a static library (libA.a) and an application (AppA) calling the library (libA.a), which is working correctly. Using gcc compiler, I made a static library (libB.a) and an application (AppB) calling the library (libB.a), which is also working correctly.

I want an application (AppA) maed on QT creator to call a library (libB.a), but the QT creator gives an error like error: undefined reference to LibBFunc(int, int)

Is there a specific way to call?

In addition, my development environment and codes are as follows.

  • Ubuntu 18.04 LTS

  • QT Creator 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;
}

compiling for library (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();
}

compiling output

../../../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.

You built the libTest.a with C compiler, and using it on C++ application. You need extern "C" { ... } in static_lib_test.h like below, to make C++ compiler (like g++) turn off the name mangling for the LibBFunc() .

#include <stdio.h> 

#ifdef __cplusplus
   extern “C” {
#endif

int  LibBFunc(int a, int b);

#ifdef __cplusplus
   }
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM