Linux 和 macOS 没问题,但 Windows 构建会抛出链接器错误 LNK2019
Linux and macOS are fine, but Windows build throws linker error LNK2019
我正在从另一个共享库中调用一个共享库中的 class 方法。应用程序在 Linux 和 macOS 上构建良好,但在 Windows 上我收到:
exportlib.obj : error LNK2019: unresolved external symbol "__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) referenced in function "private: void __cdecl Exportlib::setSuppPoints(void)" (?setSuppPoints@Exportlib@@AEAAXXZ)
debug\exportlib.dll : fatal error LNK1120: 1 unresolved externals
我 运行 不知道可能导致错误的原因(仅限 Windows!)
已导出的共享库 class:
// 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
Class声明:
// 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);
};
正在从另一个共享库中调用导出 class 的 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 不是我的情况
第一个共享库
我在第一个共享库中有一个 struct
:
// algorithmslib.h
struct Point {
float x{0}, y{0}, z{0};
};
第二个共享库
我在我的第二个共享库中使用上面的 struct
是这样的:
header:
// 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);
修复
我将第二个共享库的 header 从 class Point;
更改为 struct Point;
,Windows 上的链接器错误已解决:
// exportlib.h
struct Point; // declared in algorithmslib.h as "struct" NOT "class"
我正在从另一个共享库中调用一个共享库中的 class 方法。应用程序在 Linux 和 macOS 上构建良好,但在 Windows 上我收到:
exportlib.obj : error LNK2019: unresolved external symbol "__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) referenced in function "private: void __cdecl Exportlib::setSuppPoints(void)" (?setSuppPoints@Exportlib@@AEAAXXZ) debug\exportlib.dll : fatal error LNK1120: 1 unresolved externals
我 运行 不知道可能导致错误的原因(仅限 Windows!)
已导出的共享库 class:
// 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
Class声明:
// 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);
};
正在从另一个共享库中调用导出 class 的 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 不是我的情况
第一个共享库
我在第一个共享库中有一个 struct
:
// algorithmslib.h
struct Point {
float x{0}, y{0}, z{0};
};
第二个共享库
我在我的第二个共享库中使用上面的 struct
是这样的:
header:
// 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);
修复
我将第二个共享库的 header 从 class Point;
更改为 struct Point;
,Windows 上的链接器错误已解决:
// exportlib.h
struct Point; // declared in algorithmslib.h as "struct" NOT "class"