简体   繁体   English

Qt - 帮助链接动态库

[英]Qt - Help Linking Dynamic Library

I am having a ton of trouble with the simple task of linking a DLL to my Qt project. 将DLL连接到我的Qt项目的简单任务我遇到了很多麻烦。

My steps: 我的步骤:

  1. In Qt, I go to 'File -> New File or Project -> Other Project -> C++ Library' 在Qt中,我转到'文件 - >新文件或项目 - >其他项目 - > C ++库'
  2. Add a calc method to my mylibrary.h . calc方法添加到我的mylibrary.h中
  3. Implement the calc method in my mylibrary.cpp . 在我的mylibrary.cpp中实现calc方法。
  4. I compile, and I go to the directory where .a and .dll files were created. 我编译,然后转到创建.a.dll文件的目录。
  5. I create a new project: 'File -> New File or Project -> Qt Widget Project -> Qt Gui Application' 我创建了一个新项目:'文件 - >新文件或项目 - > Qt Widget项目 - > Qt Gui应用程序'
  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/ . 我将所有头文件从MyLibrary复制并粘贴到C:/Users/Me/includes ,以及MyLibrary.dlllibMyLibrary.a复制并粘贴到C:/Users/Me/
  7. I then go into my Qt Widget Project's project file (step 5). 然后我进入我的Qt Widget项目的项目文件(步骤5)。

I add the include path as well as the DLL path: 我添加了包含路径以及DLL路径:

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

I then go into my mainwindow.cpp , and put this code: 然后我进入我的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;
}

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文件内容

mylibrary.h contents: 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 contents: mylibrary.cpp内容:

#include "mylibrary.h"

MyLibrary::MyLibrary()
{
}

int calc()
{
    return 5;
}

I have not touched the Qt generated MyLibrary_global.h file 我没有触及Qt生成的MyLibrary_global.h文件

Thanks for any help. 谢谢你的帮助。

You define a stand alone calc function instead of MyLibrary::calc , try: 您定义一个独立的calc函数而不是MyLibrary::calc ,尝试:

mylibrary.cpp mylibrary.cpp

#include "mylibrary.h"

MyLibrary::MyLibrary()
{
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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