库路径肯定是正确的,可以创建该库的实例,但是调用任何函数时得到 "Undefined reference"

Library path is definitely correct and can create an instance of said library, but get "Undefined reference" when calling any function

我正在使用 Qt Creator 创建一个新项目,并且我有一个在 Visual Studio 中创建的测试 "MathLibrary"。我想在我的 Qt 项目中使用这个库。

我已经搜索了很多小时来寻找我的解决方案的答案,在几乎所有情况下,答案都是简单的,库没有添加到 .pro 文件的 PATH 中。我 99% 确定我所做的一切都是正确的,但是当我尝试调用这个库中的任何函数时,某些东西导致我得到一个未定义的引用错误。这是我目前所拥有的。

图书馆-

MathLibraryH.h:

#pragma once

namespace MathLibrary
{
    class Functions
    {
    public:
        // Returns a + b  
        double Add(double a, double b);

        // Returns a * b  
        double Multiply(double a, double b);

        // Returns a + (a * b)  
        double AddMultiply(double a, double b);
    };
}

MathLibrary.cpp:

#include "stdafx.h"
#include "MathLibraryH.h"

namespace MathLibrary
{
    double Functions::Add(double a, double b)
    {
        return a + b;
    }

    double Functions::Multiply(double a, double b)
    {
        return a * b;
    }

    double Functions::AddMultiply(double a, double b)
    {
        return a + (a * b);
    }
}

QT项目-

TestQTProject.pro:

    QT       += core gui \
            network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = TestQTProject
TEMPLATE = app

DEFINES += QT_DEPRECATED_WARNINGS

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


SOURCES += \
        main.cpp \
        mainwindow.cpp \

HEADERS += \
        mainwindow.h \

FORMS += \
        mainwindow.ui

DISTFILES += \
    com_github_msorvig_s3.pri

LIBS += -L$$PWD/../Libs -lMathLibrary

INCLUDEPATH += $$PWD/../Incs

mainwindow.cpp:

#include "MathLibraryH.h"

// .... other stuff ....

void MainWindow::on_btnStage1_clicked()
{
    MathLibrary::Functions lib; // This is just fine
    lib.Add(5, 9); // The "Add" function (or any other function in the library)
                       causes an undefined reference error
}

我还是 Qt 的新手,但我看不出这段代码有什么问题。

我根据搜索得到的答案尝试过的其他事情:

将以下代码添加到 MathLibrary.h:

#ifdef MATHLIBRARY_EXPORTS  
#define MATHLIBRARY_API __declspec(dllexport)   
#else  
#define MATHLIBRARY_API __declspec(dllimport)   
#endif  

将 .pro 文件中的 LIBS 声明格式更改为以下所有格式:

多行:

LIBS += -L$$PWD/../Libs
LIBS += -lMathLibrary

硬编码单行:

LIBS += -LC:\svn\software\WIP\TestQTProject\Libs -lMathsLibrary

我所做的一切都行不通,我也没有其他想法了。

就其价值而言,该库在使用 visual studio 创建的任何项目中都能正常工作,我已经尝试创建静态库和动态库。

添加

MATHLIBRARY_API

在您的 class 名称前面导出您的 dll 中的所有 public 成员。在导出期间应设置 dllexport 部分,在导入期间当然要设置 dllimport 部分。

这里有一个 link 解释了如何从 dll 中导出 classes: https://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL

如果您试图 link 动态,问题是您的库 MathsLibrary 没有导出为 动态库 ,而且您也没有将其作为 动态库 导入(这就是 __declspec(dllexport) 所做的,并且应该在函数或 类 声明的前面使用宏以有所不同)。即使您这样做了,您编译它的方式也有可能与您的 Qt 应用程序不同,因此存在 linking 问题。

但是由于您正在尝试 link 静态 (我没有在您的代码中看到任何库文件加载),问题似乎是两者之间的差异库和 Qt 应用程序的编译。

您应该尝试使用与构建您的库相同的标志和相同的编译器来构建您的 Qt 应用程序,反之亦然。如果您的代码如您所说的那样工作,它可能会工作。

更多详情:

DL Library

How to Create a Plugin Framework

Building plugins using Qt Framework