Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ DLL-Linking UnResolved Externals

Tags:

c++

dllexport

I have a rather big Core project that I'm working with, I'm attempting to adapt it to use a DLL Engine I've built, I'm getting a bunch of errors like:

unresolved external symbol "private: static class

When including some of the headers from the Core in the DLL, the class is exported via __declspec(dllexport) but any header with static members throws out a crapload of errors regarding the static members.

This is a rather big project, I can't exactly run around removing every static class member I see, is there anyway around this kind of thing?

A basic example of a class that's being imported:

class __declspec(dllexport) MyClass
{
    public:
        static bool m_someVar;
}

For clarity sake I'd just like to address that m_someVar is defined/declared (forget the term) in the classes implementation file

like image 909
Undawned Avatar asked Jan 22 '23 05:01

Undawned


1 Answers

When you compile the Core you want these functions to be dllexport; However, when you compile the DLL, you want them to be dllimport. In your case, you're always defining them as dllexport, thus when you link the DLL it complains that you've declared a function (and even said you'd export it) without ever defining it.

The solution is simple. Instead of manually __declspecing, create a macro based on whether you're the Core or the DLL:

#ifndef I_AM_A_DLL
#define EXPORT __declspec(dllexport)
#define IMPORT __declspec(dllimport)
#else
#define EXPORT __declspec(dllimport)
#define IMPORT __declspec(dllexport)
#endif

Use EXPORT for functions in the Core and IMPORT for functions in external DLLs:

class EXPORT MyClass
{
    public:
        static bool m_someVar;
}
like image 89
MoshiBin Avatar answered Jan 30 '23 06:01

MoshiBin