简体   繁体   中英

Qt - Help Linking Dynamic Library

I am having a ton of trouble with the simple task of linking a DLL to my Qt project.

My steps:

  1. In Qt, I go to 'File -> New File or Project -> Other Project -> C++ Library'
  2. Add a calc method to my mylibrary.h .
  3. Implement the calc method in my mylibrary.cpp .
  4. I compile, and I go to the directory where .a and .dll files were created.
  5. I create a new project: 'File -> New File or Project -> Qt Widget Project -> Qt Gui Application'
  6. I copy and paste all header files from MyLibrary to C:/Users/Me/includes , as well as MyLibrary.dll and libMyLibrary.a to C:/Users/Me/ .
  7. I then go into my Qt Widget Project's project file (step 5).

I add the include path as well as the DLL path:

INCLUDEPATH += "C:/Users/Me/includes"
LIBS += "C:/Users/Me/MyLibrary.dll"

I then go into my mainwindow.cpp , and put this code:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include "mylibrary.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    MyLibrary myLib;
    qDebug() << myLib.calc();
}

MainWindow::~MainWindow()
{
    delete ui;
}

And of course, I get undefined errors:

C:\Users\Me\Desktop\TestInternals-Win\..\TestInternals\mainwindow.cpp:15: error: undefined reference to `_imp___ZN13MyLibrary4calcEv'

C:\Users\Me\Desktop\TestInternals-Win\..\TestInternals\mainwindow.cpp:15: error: undefined reference to `_imp___ZN13MyLibrary4calcEv'

:-1: error: collect2: ld returned 1 exit status

MyLibrary File Contents

mylibrary.h contents:

#ifndef MYLIBRARY_H
#define MYLIBRARY_H

#include "MyLibrary_global.h"

class MYLIBRARYSHARED_EXPORT MyLibrary{
public:
    MyLibrary();
    int calc();
};

#endif // MYLIBRARY_H

mylibrary.cpp contents:

#include "mylibrary.h"

MyLibrary::MyLibrary()
{
}

int calc()
{
    return 5;
}

I have not touched the Qt generated MyLibrary_global.h file

Thanks for any help.

You define a stand alone calc function instead of MyLibrary::calc , try:

mylibrary.cpp

#include "mylibrary.h"

MyLibrary::MyLibrary()
{
}

int MyLibrary::calc()
{
    return 5;
}

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