跨dll使用静态类变量/函数 [英] use static class variable/function across dlls

查看:46
本文介绍了跨dll使用静态类变量/函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助访问跨 DLL/主程序的全局函数.我有一个 Base 类

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_DLL)

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

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

当我从 dll 访问时:

When I access from the dll:

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

谢谢,加西姆

推荐答案

这取决于系统,但您必须确保包含成员函数定义和静态成员数据的 DLL 中的符号正确导出符号,并且使用它们的 DLL 会正确导入它们.在 Linux 下,这意味着在链接可执行文件时使用 -E 选项(如果在可执行文件中定义了符号);在 Windows 下,您通常必须使用条件编译的编译器扩展,参见 __declspec;Microsoft 编译器不支持标准 C++ 中的 DLL.

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++.

这是一个适用于我的系统 (VC 2010) 的示例:

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

在啊:

#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

在 A.cpp 中:

#include "A.h"

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

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

在 main.cpp 中:

In main.cpp:

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

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

编译和链接:

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

据我所知(我更像是一个 Unix 人),所有的 .cpp成为 dll 的一部分应该在命令行中有/DDLL_A调用编译器;其他人都不应该.在 Visual Studio 中,这通常是通过为每个 dll 使用单独的项目来实现的,并且每个可执行文件.在项目的属性中,有一个条目配置属性→C/C++→预处理器→预处理器定义;只需在那里添加 DLL_A(但仅在一个项目中生成A.ddl).

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).

这篇关于跨dll使用静态类变量/函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆