【问题标题】:Linux and macOS are fine, but Windows build throws linker error LNK2019Linux 和 macOS 都很好,但 Windows 构建会引发链接器错误 LNK2019
【发布时间】:2019-04-29 06:52:55
【问题描述】:

我正在从另一个共享库中调用共享库中的类方法。应用程序在 Linux 和 macOS 上构建良好,但在 Windows 上我收到:

exportlib.obj : 错误 LNK2019: 无法解析的外部符号“__declspec(dllimport) public: class std::vector > __cdecl Algorithmslib::k_means(class std::vector > const &,unsigned __int64,unsigned __int64)” (__imp_ ?k_means@Algorithmslib@@QEAA?AV?$vector@VPoint@@V?$allocator@VPoint@@@std@@@std@@AEBV23@_K1@Z) 在函数“private: void __cdecl Exportlib::setSuppPoints”中引用(void)" (?setSuppPoints@Exportlib@@AEAAXXZ) debug\exportlib.dll : 致命错误 LNK1120: 1 unresolved externals

我想不出可能导致错误的原因(仅在 Windows 上!)


导出类的共享库:

// Project file for exported shared library
// algorithmslib.pro

DEFINES += ALGORITHMSLIB_LIBRARY

根据定义导出或导入:

// algorithmslib_global.h

#if defined(ALGORITHMSLIB_LIBRARY)
#  define ALGORITHMSLIBSHARED_EXPORT Q_DECL_EXPORT
#else
#  define ALGORITHMSLIBSHARED_EXPORT Q_DECL_IMPORT
#endif

类声明:

// algorithmslib.h

#include "algorithmslib_global.h"

class ALGORITHMSLIBSHARED_EXPORT Algorithmslib : public QObject
{
    Q_OBJECT

public:
    Algorithmslib();
    std::vector<Point> k_means(const std::vector<Point>& data,
                               size_t k,
                               size_t number_of_iterations);
};

从另一个共享库中调用导出类的k_means 方法:

// This is a second shared library which calls the exported class of the 1st shared library
// exportlib.cpp

#include "algorithmslib.h"

void Exportlib::setSuppPoints()
{
    Algorithmslib algorithmEngine;
    std::vector<Point> means = algorithmEngine.k_means(data, k, number_of_iterations);
}

我正在使用 Desktop_Qt_5_12_1_MSVC2017_64bit-Debug 工具包进行编译:


我将共享库重命名为非常独特的名称,但仍引发相同的链接器错误。因此,this issue 不是我的情况

【问题讨论】:

  • k_means 函数定义了吗?
  • 你如何构建你的应用程序?构建命令行会很有趣。如果它建立在 linux/mac 上,它也应该建立在 windows 上。你在用视觉工作室吗?
  • @vahancho 是的,k_means 是在源代码中实现的,这就是为什么在 Linux 和 macOS 上构建很好:)
  • @AF_cpp 我正在使用 Qt Creator。我已经删除了构建目录并多次重复构建过程,但没有运气:(
  • @user3405291,当你调用一个只有声明而没有定义的函数时,一些链接器不会抱怨。在这种情况下,您只能在运行时检测到问题。

标签: c++ qt linker linker-errors


【解决方案1】:

第一个共享库

我在第一个共享库中有一个struct

// algorithmslib.h

struct Point {
    float x{0}, y{0}, z{0};
};

第二个共享库

我在我的第二个共享库中使用上述struct,如下所示:

标题:

// exportlib.h

class Point; // Note that I was using "class" here 
             // but the declaration inside algorithmslib.h is "struct"

size_t computeNumberOfClusters(const std::vector<Point> &data);

来源:

//exportlib.cpp

#include "algorithmslib.h"

std::vector<Point> data(number_of_points); // Point structure is declared in algorithmslib.h

size_t k = computeNumberOfClusters(data);
size_t number_of_iterations = 300;
Algorithmslib algorithmEngine;

std::vector<Point> means = algorithmEngine.k_means(data, k, number_of_iterations);

修复

我将第二个共享库的标头从 class Point; 更改为 struct Point;,Windows 上的链接器错误得到解决:

// exportlib.h

struct Point; // declared in algorithmslib.h as "struct" NOT "class"

【讨论】:

  • 我想知道为什么 Linux (gcc-g++) 和 macOS (clang++) 不关心 classstruct 不匹配!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 2015-06-28
  • 2015-05-01
  • 2022-06-23
  • 2023-03-30
  • 2015-06-30
  • 2017-04-07
相关资源
最近更新 更多