Can't link my library



  • I made a library in qt creator as c++ dynamic library option. But now i can' compile the project im using it.

    my .pro file is this;
    QT += core gui multimedia

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = testlib2
    TEMPLATE = app

    DEFINES += QT_DEPRECATED_WARNINGS

    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

    CONFIG += c++11

    SOURCES +=
    main.cpp
    mainwindow.cpp

    HEADERS +=
    mainwindow.h

    FORMS +=
    mainwindow.ui

    Default rules for deployment.

    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target

    unix|win32: LIBS += -L$$PWD/../build-tonegenlib-Desktop_Qt_5_12_1_MinGW_64_bit-Debug/debug/ -ltonegenlib

    INCLUDEPATH += $$PWD/../tonegenlib
    DEPENDPATH += $$PWD/../tonegenlib

    and the compile errror is this ;

    debug/mainwindow.o: In function MainWindow::MainWindow(QWidget*)': C:\QT_workspace\build-testlib2-Desktop_Qt_5_12_1_MinGW_64_bit-Debug/../testlib2/mainwindow.cpp:6: undefined reference to__imp__ZN10TonegenlibC1Ev'
    collect2.exe: error: ld returned 1 exit status
    mingw32-make[1]: *** [Makefile.Debug:69: debug/testlib2.exe] Error 1
    mingw32-make: *** [Makefile:38: debug] Error 2
    mingw32-make[1]: Leaving directory 'C:/QT_workspace/build-testlib2-Desktop_Qt_5_12_1_MinGW_64_bit-Debug'
    01:29:22: The process "C:\Qt\Qt5.12.1\Tools\mingw730_64\bin\mingw32-make.exe" exited with code 2.
    Error while building/deploying project testlib2 (kit: Desktop Qt 5.12.1 MinGW 64-bit)
    When executing step "Make"
    01:29:22: Elapsed time: 00:01.

    thank you so much for your help.



  • @Sareban
    Where exactly is your tonelib file (full name & path) on the disk?
    Do you build it yourself?
    Are you sure it's MinGW and not, say, MSVC?
    Are you sure it's 64-bit and not 32-bit?
    Have you included the right header file for it, and are you calling a function in it which is correctly exported?



  • @JonB
    thanks for your reply. I supplied the full name and path to the .pro file with add library thing on qt. and i built it and qmake with the same kit and compiler i do with every other project. add library seeks for .a file and i provided it with that in the same folder of debug .dll .



  • @JonB
    #include <QMainWindow>
    #include <tonegenlib.h>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    Tonegenlib tonegen;
    
    
    
    void test();
    

    private:
    Ui::MainWindow *ui;
    };

    this is the header file of the project.

    void MainWindow::test()
    {
    tonegen.m_freq = 240;
    tonegen.playAudio();

    }

    and this is the .cpp where i call the function and change a variable.
    without using the library there is no errors but when i define tonegen and then call the function gives error.



  • Hi, can you show the tonegenlib.h file?



  • @hskoglund

    #ifndef TONEGENLIB_H
    #define TONEGENLIB_H

    #include "tonegenlib_global.h"
    #include <QBuffer>
    #include <QByteArray>
    #include <QMainWindow>
    #include <QAudio>
    #include <QtMultimedia/QAudioOutput>
    #include <QScopedPointer>
    #include <QFile>
    #include <QObject>
    #include <QFile>
    #include <QDataStream>
    #include <QIODevice>
    #include <QtMultimedia/QAudioDeviceInfo>
    #include <QtMultimedia/QAudioFormat>

    class TONEGENLIBSHARED_EXPORT Tonegenlib : public QWidget
    {
    Q_OBJECT

    public:
    Tonegenlib();
    void toneGenerate();
    void playAudio();
    void saveSound();
    void readFile();
    int SAMPLE_RATE = 44100;
    int m_seconds=2;
    int m_freq=50;
    int m_ph;
    int m_amp = 5;

    private:

    int len= m_seconds*SAMPLE_RATE;
    
    QAudioFormat format;
    QByteArray sinbuf;
    QBuffer sininput;
    QAudioOutput *audio;
    

    private slots:
    void handleStateChanged(QAudio::State newState);
    void supplyData();
    };

    #endif // TONEGENLIB_H

    this is my tonelib.h



  • Sorry, I was looking for where you #define TONEGENLIBSHARED_EXPORT, can you show the tonegenlib_global.h?



  • @hskoglund

    #ifndef TONEGENLIB_GLOBAL_H
    #define TONEGENLIB_GLOBAL_H

    #include <QtCore/qglobal.h>

    #if defined(TONEGENLIB_LIBRARY)

    define TONEGENLIBSHARED_EXPORT Q_DECL_EXPORT

    #else

    define TONEGENLIBSHARED_EXPORT Q_DECL_IMPORT

    #endif

    #endif // TONEGENLIB_GLOBAL_H

    thanks for your help :)



  • Hmm, just a guess but it seems Tonegenlib's constructor isn't visible, can you show the code for Tonegenlib::Tonegenlib in your tonelib.cpp file?



  • @hskoglund

    oh ok. do i need a constructor and deconstructor in a shared library cpp file ? didnt know that. i just started the code with the first function needed.

    void Tonegenlib::toneGenerate() i started like this.

    is there anything i need to consider when writing the constructor of the cpp ?

    or is it enough to start with tonegenlib::tonegenlib()



  • @Sareban You don't need a constructor because it's a shared library, but since you mention the constructor in tonelib.h:

    ...
    public:
    Tonegenlib();
    ...
    

    either define an empty constructor in tonelib.cpp Tonegenlib::Tonegenlib() {} or remove the line from tonelib.h.



  • @hskoglund
    thank you so much :) first tried to define an empty constructor but it didnt work but then when i cleared the line from the .h it worked. thanks so much :))


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.