Unresolved external symbol in a DLL
-
I have an awful lot of deployed code that uses this mechanism - here's a sample:
#if!defined(COMMONLIB_API) #ifdef COMMONLIB_EXPORTS #define COMMONLIB_API __declspec(dllexport) #else #define COMMONLIB_API __declspec(dllimport) #endif #endif namespace MyAppLibrary { class MessageReporter { public: //ctor COMMONLIB_API MessageReporter(); //Add new reporting function COMMONLIB_API static void AddReportFunction(ReportFnPtr func); //Set MessageReporter Level on or off COMMONLIB_API static void SetReporterLevel(Level lev, bool on); //Set all levels to on COMMONLIB_API static void AllLevelsOn() {repLevels=~0;} //Set all levels to off COMMONLIB_API static void AllLevelsOff() {repLevels=0;} }; };
Is it possible you haven't exported necessary constructors?But if you only expose class not expose methods, the methods could not be found when linking the client of the DLL (link error, unresolved symbols related to the methods). Could you have a try? I have tried. regards, George
-
But if you only expose class not expose methods, the methods could not be found when linking the client of the DLL (link error, unresolved symbols related to the methods). Could you have a try? I have tried. regards, George
DLL interface defined in a.h
a.h
#if !defined(__A_H__) #define __A_H__ #if!defined(A_API) #ifdef A_EXPORTS #define A_API __declspec(dllexport) #else #define A_API __declspec(dllimport) #endif #endif class A_API A { public: A(); A(int a); void DoSomething(int b); int Result() const; private: int a_; }; #endif // !defined(__A_H__)
DLL implemented in a.cpp and built withcl -EHsc -LD a.cpp
a.cpp
#include <windows.h> #define A_EXPORTS #include "a.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } A::A() : a_(0) {} A::A(int a) : a_(a) {} void A::DoSomething(int b) { a_ += b; } int A::Result() const { return a_; }
DLL used in b.cpp, built withcl -EHsc b.cpp a.lib
:b.cpp
#include <iostream> #include "a.h" int main(int argc, char** argv) { A a(argc); a.DoSomething(3); std::cout << a.Result() << std::endl; }
Builds fine, runs OK and even produces the right result...which is nice.modified on Saturday, December 20, 2008 6:19 AM
-
DLL interface defined in a.h
a.h
#if !defined(__A_H__) #define __A_H__ #if!defined(A_API) #ifdef A_EXPORTS #define A_API __declspec(dllexport) #else #define A_API __declspec(dllimport) #endif #endif class A_API A { public: A(); A(int a); void DoSomething(int b); int Result() const; private: int a_; }; #endif // !defined(__A_H__)
DLL implemented in a.cpp and built withcl -EHsc -LD a.cpp
a.cpp
#include <windows.h> #define A_EXPORTS #include "a.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; } A::A() : a_(0) {} A::A(int a) : a_(a) {} void A::DoSomething(int b) { a_ += b; } int A::Result() const { return a_; }
DLL used in b.cpp, built withcl -EHsc b.cpp a.lib
:b.cpp
#include <iostream> #include "a.h" int main(int argc, char** argv) { A a(argc); a.DoSomething(3); std::cout << a.Result() << std::endl; }
Builds fine, runs OK and even produces the right result...which is nice.modified on Saturday, December 20, 2008 6:19 AM
Cool, Stuart! regards, George