Consider the following source of From.dll library defining the From class:
Header:
// From.h
#include <afxwin.h>
#include <afxext.h>
class AFX_EXT_CLASS From
{
public:
static const char staticMember[666];
};
Implementation:
// From.cpp
#include "From.h"
const char From::staticMember[] = "Total Crap";
This library static member staticMember must be used from the following To class, which must also give rise to a DLL (To.dll):
Header:
// To.h
#include <afxwin.h>
#include <afxext.h>
class AFX_EXT_CLASS To
{
public:
static const char* retExtStatic();
};
Implementation:
// To.cpp
#include "To.h"
#include "From.h"
const char* To::retExtStatic()
{
return From::staticMember;
}
But I keep getting this LNK2001: unresolved external symbol "public: static char const * const From::staticMember" (?staticMember#From##2QBDB) when linking To.dll, no matter if it's directly on Visual Studio or if it's a cmake generated nmake:
Linking CXX shared library To.dll
Creating library To.lib and object To.exp
To.cpp.obj : error LNK2001: unresolved external symbol "public: static char const * const From::staticMember" (?staticMember#From##2QBDB)
To.dll : fatal error LNK1120: 1 unresolved externals
The first DLL is compiled and linked allright. The following are the relevant parts of my CMakeLists.txt:
add_definitions (-D_AFXDLL -D_AFXEXT -DPSAPI_VERSION=1)
set (CMAKE_MFC_FLAG 2) # Use MFC in a shared DLL
# FROM
file (GLOB FROM ${CMAKE_CURRENT_SOURCE_DIR}/From.cpp)
add_library (From SHARED ${FROM})
target_link_libraries (From)
# TO
file (GLOB TO ${CMAKE_CURRENT_SOURCE_DIR}/To.cpp)
add_library (To SHARED ${TO})
target_link_libraries (To From)
What am I doing wrong to export this static data member?
As #Niall and #Igor Tnadetnik pointed out on comments on the question, when a DLL needs another DLL, it's not safe to include the header file of the DLL to be used if therein the class is declared with AFX_EXT_CLASS. In this case, as the client code is also a DLL, AFX_EXT_CLASS would be defined as __declspec(dllexport) and not as __declspec(dllimport) as it should.
Related
I am working on a cross-platform (Linux & Windows) library. One of my class has a static member defined in the header. And I created it in a CPP file like
namespace raisim {
std::function<void()> RaiSimMsg::fatalCallback_ = []() { exit(1); };
}
The header file
#ifndef RAISIM_MESSAGE_LOGGER_HPP
#define RAISIM_MESSAGE_LOGGER_HPP
#include <chrono>
...
namespace raisim {
class RaiSimMsg {
public:
...
void stream(const char *file, const int line, std::stringstream &msg, int severity) {
if (severity == RSEVERITY_FATAL)
fatalCallback_();
}
private:
static std::function<void()> fatalCallback_;
};
}
#endif //RAISIM_MESSAGE_LOGGER_HPP
This works perfectly with GCC and Clang in Linux. But MSVC is giving a link error. Does MSVC have its own rules for static members??
Full error log
1>anymal.obj : error LNK2001: unresolved external symbol "private: static class std::function<void __cdecl(void)> raisim::RaiSimMsg::fatalCallback_" (?fatalCallback_#RaiSimMsg#raisim##0V?$function#$$A6AXXZ#std##A)
1>C:\Users\ultrafrog\source\repos\raisim\build\benchmark\Debug\speed_test_anymal.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "speed_test_anymal.vcxproj" -- FAILED.
Jack Dingler's answer here
https://www.codeproject.com/Questions/585271/Aplusstaticplusmemberplusvariableplusexportpluserr
worked. So the issue is that the Windows linker needs to know that if it has to import or export the symbol. So we have to declare __declspec(dllexport) when we export (when we build the library) and __declspec(dllimport) for using it. We can switch that using a compilation flag. I am not sure why they designed the linker like this but anyway it works now.
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!)
Shared library with exported class:
// 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);
};
Calling 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
First shared lib
I have a struct inside 1st shared library:
// algorithmslib.h
struct Point {
float x{0}, y{0}, z{0};
};
Second shared lib
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);
Fix
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"
I'm having a hard time compiling a C/C++ dll on Visual Studio. It seems to be linked to CRT library.
A simple VS c++ dll project. Just removed recompiled header, and added /NOENTRY. Exporting methods is working, but a simple class (not intended to be exported) raises a lot of linking error with CRT library:
Test.h:
class ITest
{
public:
virtual void foo() = 0;
}
class Test final : public ITest
{
public:
Test();
~Test();
void foo();
};
// extern "C" __declspec(dllexport) ITest* __cdecl CreateTest();
Test.cpp
#include "Test.h"
Test::Test() {}
Test::~Test() {}
Test::foo() {}
// extern "C" __declspec(dllexport) ITest* __cdecl CreateTest() { return new Test; }
errors:
Test.obj : error LNK2001: unresolved external symbol _purecall
msvcrt.lib(delete_scalar.obj) : error LNK2019: unresolved external symbol free referenced in function "void __cdecl operator delete(void *)" (??3#YAXPEAX#Z)
Uncommenting the export raises more msvcrt.lib link errors and changing the runtime library for MDd, MT, MTd changes only the library referenced.
Thanks you for any help :)
The problem comes from /NOENTRY option in Project Properties->Linker->Advanced->No Entry Point. Removing it solved the problem
/NOENTRY is for creating dlls which don't have any code at all and only contiain bitmaps etc: https://learn.microsoft.com/en-us/cpp/build/creating-a-resource-only-dll?view=vs-2017
As the dll (should) have no code visual studio doesn't link in the runtime library either, but as there is code it generates requirements for the runtime library symbols which are unresolved.
all
i created a static lib project with VS 2010 sp1, and simply defined a class with a member function (code snippet):
.h:
namespace puphttp{
class CRequester
{
public:
void RequesterUpdate();
};
}
.cpp:
#include "StdAfx.h"
#include "Requester.h"
#include <iostream>
using namespace std;
namespace puphttp{
void CRequester::RequesterUpdate()
{
cout<<"updating";
}
}
at last i linked the lib file and tried to invoke the following code:
puphttp::CRequester c;
c.RequestUpdate();
the following link error occur as i compile:
error LNK2001: unresolved external symbol "public: void __cdecl puphttp::CRequester::RequestUpdate(void)" (?RequestUpdate#CRequest#puphttp##QEAAXXZ)
then i used dumpbin to check the actual function name in my lib, it's:
(?RequestUpdate#CRequest#puphttp##QAEXXZ)
so the difference is QEAAXXZ vs QAEXXZ, i didn't have time to get into name mangling rule yet, so any quick answer? really much appreciated.
I am trying to create a rather simple project in native c++ that calls a a managed dll.
this how my native c++ code looks:
// MyCppStud.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "MyStudWrapper\MyStudentWrapperWrapper.h"
int _tmain(int argc, _TCHAR* argv[])
{
char * path = "C:/Users/rami.schreiber/documents/visual studio 2013/Projects/TestProj/test.xml";
MyStudentWrapperWrapper* student = new MyStudentWrapperWrapper();
// student->GetStudent(path);
return 0;
}
and here are the .h and .cpp files for the managed dll (compiled with /clr)
//MyStudentWrapperWrapper.h
#pragma once
//#ifdef THISDLL_EXPORTS
#define THISDLL_API __declspec(dllexport)
/*#else
#define THISDLL_API __declspec(dllimport)
#endif*/
class MyStudentWrapper;
class THISDLL_API MyStudentWrapperWrapper
{
private:
MyStudentWrapper* _Impl;
public:
MyStudentWrapperWrapper();
MyStudentWrapperWrapper(MyStudentWrapper* student);
~MyStudentWrapperWrapper();
MyStudentWrapperWrapper* GetStudent(const char* path);
void SaveStudent(MyStudentWrapperWrapper* student, const char* path);
};
// MyStudentWrapperWrapper.cpp
#pragma once
#include "stdafx.h"
#include "MyStudWrapper.h"
#include "MyStudentWrapperWrapper.h"
MyStudentWrapperWrapper::MyStudentWrapperWrapper()
{
_Impl = new MyStudentWrapper;
}
when i build the solution i get a link error
1>MyCppStud.obj : error LNK2019: unresolved external symbol "public: __thiscall MyStudentWrapperWrapper::MyStudentWrapperWrapper(void)" (??0MyStudentWrapperWrapper##QAE#XZ) referenced in function _main
1>c:\users\...\documents\visual studio 2013\Projects\MyStudentProj\Debug\MyCppStud.exe : fatal error LNK1120: 1 unresolved externals
From what I understand I am not referencing the the .lib file correctly and therefor the linker does not recognize the c'tor for my wrapper class.
can someone please explain how to correctly reference the dll to my c++ project.
thank you very much!