将DLL链接到我的Qt项目这个简单的任务我遇到了很多麻烦。
我的步骤:
calc
方法。calc
方法。.a
和.dll
文件的目录。C:/Users/Me/includes
,以及MyLibrary.dll
和libMyLibrary.a
复制并粘贴到C:/Users/Me/
。我添加了包含路径以及DLL路径:
INCLUDEPATH += "C:/Users/Me/includes"
LIBS += "C:/Users/Me/MyLibrary.dll"
然后我进入我的mainwindow.cpp
,并输入以下代码:
#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;
}
当然,我得到了未定义的错误:
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.h 内容:
#ifndef MYLIBRARY_H
#define MYLIBRARY_H
#include "MyLibrary_global.h"
class MYLIBRARYSHARED_EXPORT MyLibrary{
public:
MyLibrary();
int calc();
};
#endif // MYLIBRARY_H
mylibrary.cpp 内容:
#include "mylibrary.h"
MyLibrary::MyLibrary()
{
}
int calc()
{
return 5;
}
我没有触及Qt生成的MyLibrary_global.h文件
感谢您的帮助。
答案 0 :(得分:3)
您定义了独立的calc
功能,而不是MyLibrary::calc
,请尝试:
mylibrary.cpp
#include "mylibrary.h"
MyLibrary::MyLibrary()
{
}
int MyLibrary::calc()
{
return 5;
}