DLLをQtプロジェクトにリンクするという単純なタスクで多くの問題が発生しています。
私のステップ:
- Qtで、[ファイル]->[新しいファイルまたはプロジェクト]->[その他のプロジェクト]->[C++ライブラリ]に移動します。
- mylibrary.h
calc
にメソッドを追加します。 - mylibrary.cpp
calc
にメソッドを実装します。 - コンパイルして、
.a
ファイル.dll
が作成されたディレクトリに移動します。 - 新しいプロジェクトを作成します:'ファイル->新しいファイルまたはプロジェクト->Qtウィジェットプロジェクト->QtGuiアプリケーション'
- MyLibraryから
C:/Users/Me/includes
、MyLibrary.dll
およびlibMyLibrary.a
にすべてのヘッダーファイルをコピーして貼り付けますC:/Users/Me/
。 - 次に、Qtウィジェットプロジェクトのプロジェクトファイルに移動します(ステップ5)。
インクルードパスと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ファイルの内容
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ファイルには触れていません
助けてくれてありがとう。