use static class variable/function across dlls

2019-01-15 21:53发布

问题:

I need help access global functions across DLLs/main program. I have a class Base

Base.h

#ifdef MAIN_DLL
#define DECLSPEC __declspec(dllexport)
#else
#define DECLSPEC __declspec(dllimport)
#endif


class Base {
private:
    DECLSPEC static Filesystem * filesystem;
    DECLSPEC static Logger * logger;
    DECLSPEC static System * system;

public:

    static void setFilesystem(Filesystem * filesystem_);
    static void setApplication(Application * application_);
    static void setLogger(Logger * logger_);
    static void setSystem(System * system_);

    static Filesystem * fs() { return filesystem; }
    static Logger * log() { return logger; }
    static System * sys() { return system; }

};

main.cpp (main application) (MAIN_DLL is predefined here)

Filesystem * Base::filesystem = 0;
Logger * Base::logger = 0;
System * Base::system = 0;

When I access from the dll:

System * system = Base::sys();
if(system == 0) std::cout << "Error";

Thanks, Gasim

回答1:

This is system dependent, but you'll have to ensure that the symbols in the DLL containing the definitions of the member functions and static member data correctly exports the symbols, and that the DLL using them correctly imports them. Under Linux, this means using the -E option when linking the executable (if the symbols are defined in the executable); under Windows, you usually have to use conditionally compiled compiler extensions, see __declspec; Microsoft compilers do not support DLL's in standard C++.

EDIT:

Here's an example that works on my system (VC 2010):

In A.h:

#ifndef A_h_20111228AYCcNClDUzvxOX7ua19Fb9y5
#define A_h_20111228AYCcNClDUzvxOX7ua19Fb9y5

#include <ostream>

#ifdef DLL_A
#define A_EXPORT __declspec(dllexport)
#else
#define A_EXPORT __declspec(dllimport)
#endif

class A_EXPORT InA
{
    static std::ostream* ourDest;
public:
    static void setDest( std::ostream& dest );
    static std::ostream* getStream() { return ourDest; }
};
#endif

In A.cpp:

#include "A.h"

std::ostream* InA::ourDest = NULL;

void
InA::setDest( std::ostream& dest )
{
    ourDest = &dest;
}

In main.cpp:

#include <iostream>
#include "A.h"

int
main()
{
    InA::setDest( std::cout );
    std::cout << InA::getStream() << std::endl;
    return 0;
}

Compiled and linked with:

cl /EHs /LDd /DDLL_A A.cpp
cl /EHs /MDd main.cpp A.lib

As I understand it (I'm more a Unix person), all of the .cpp which become part of the dll should have /DDLL_A in the command line which invokes the compiler; none of the others should. In Visual Studios, this is usually achieved by using separate projects for each dll and each executable. In the properties for the project, there's an entry ConfigurationProperties→C/C++→Preprocessor→Preprocessor Definitions; just add DLL_A there (but only in the one project that generates A.ddl).



回答2:

The problem is that your header file meant for compilation into a DLL contains code! So "main.exe" exectutes a local copy of the inline functions (such as Base::sys), but the actual implementation of "Base::setSystem" is compiled into the DLL. So when main invokes the "setSystem" call, it calls the Base::setSystem linked into the DLL. But when it compiles Base::sys, it sees that an inline implementation exists and uses that.

In other words, you have two copies of "Base" floating around. One that lives in the EXE and another that lives in the DLL. And the inline functions confuse the compiler and linker as to which version to invoke.

Don't put inline functions (or code for that matter) in a header file where the implementation is meant to live in a DLL.

Easy fix:

// base.h (gets included by main)
class Base {
private:
static Filesystem * filesystem;
    static Logger * logger;
    static System * system;

public:

    static void setFilesystem(Filesystem * filesystem_);
    static void setApplication(Application * application_);
    static void setLogger(Logger * logger_);
    static void setSystem(System * system_);
    static Filesystem * fs();
    static Logger * log();
    static System * sys();
};

// base.cpp (gets compiled only within the DLL
System* Base::sys()
{
    return system;
}

// repeat "get" function for "log" and "fs" as well

Right fix:

You really, really, really should not be attempting to export C++ classes from DLLs. It's allowed, but gets complicated really quick when you start inlining code in header files and changing interfaces in different versions of the DLL.

A better approach is to export a pure "C" library from a DLL. And don't expose the internals in header files. OR, if you do want to export C++ classes, do it with COM interfaces. Then all you are doing is putting an interface declaration into your header file.