Reputation: 7938
I'm calling a class method in a shared library from within another shared library. Application builds fine on Linux and macOS but on Windows I receive:
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
I ran out of ideas what might cause the error (only on Windows!)
// Project file for exported shared library
// algorithmslib.pro
DEFINES += ALGORITHMSLIB_LIBRARY
Export or import according to defines:
// algorithmslib_global.h
#if defined(ALGORITHMSLIB_LIBRARY)
# define ALGORITHMSLIBSHARED_EXPORT Q_DECL_EXPORT
#else
# define ALGORITHMSLIBSHARED_EXPORT Q_DECL_IMPORT
#endif
Class declaration:
// 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
method of exported class from within another shared library:// 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);
}
I'm compiling with Desktop_Qt_5_12_1_MSVC2017_64bit-Debug kit:
I renamed the shared library to something very unique, and yet the same linker error is thrown. Therefore, this issue is NOT my case
Upvotes: 0
Views: 503
Reputation: 7938
I have a struct
inside 1st shared library:
// algorithmslib.h
struct Point {
float x{0}, y{0}, z{0};
};
I was using the above struct
inside my 2nd shared library like this:
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);
source:
//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);
I changed the header for 2nd shared library from class Point;
to struct Point;
and linker error on Windows got resolved:
// exportlib.h
struct Point; // declared in algorithmslib.h as "struct" NOT "class"
Upvotes: 1