36

我有一个 DLL,其中包含一个带有静态成员的类。我使用__declspec(dllexport)是为了利用这个类的方法。但是当我将它链接到另一个项目并尝试编译它时,我得到静态数据的“未解析的外部符号”错误。

例如在 DLL 中,Test.h

class __declspec(dllexport) Test{
protected:
    static int d;
public:
    static void m(){int x = a;}
}

在 DLL 中,Test.cpp

#include "Test.h"

int Test::d;

在使用 Test 的应用程序中,我调用 m()。

我还尝试对每种方法分别使用 __declspec(dllexport) ,但静态成员仍然出现相同的链接错误。

如果我使用 dumpbin 检查 DLL(.lib),我可以看到符号已被导出。

例如,应用程序在链接时给出以下错误:

1>Main.obj : error LNK2001: unresolved external symbol "protected: static int CalcEngine::i_MatrixRow" (?i_MatrixRow@CalcEngine@@1HA)

但是 .lib 的垃圾箱包含:

Version      : 0
  Machine      : 14C (x86)
  TimeDateStamp: 4BA3611A Fri Mar 19 17:03:46 2010
  SizeOfData   : 0000002C
  DLL name     : CalcEngine.dll
  Symbol name  : ?i_MatrixRow@CalcEngine@@1HA (protected: static int CalcEngine::i_MatrixRow)
  Type         : data
  Name type    : name
  Hint         : 31
  Name         : ?i_MatrixRow@CalcEngine@@1HA

我不知道如何解决这个问题。我究竟做错了什么?我怎样才能克服这些错误?

PS 该代码最初是为 Linux 开发的,.so/binary 组合可以正常工作

编辑:在给定的情况下,应用程序不直接引用静态变量,但该方法是内联的,因为它在标题中。我能够通过将方法移动到 .cpp 文件来解决链接错误。

4

5 回答 5

19

在cprogramming.com 的这个线程中,建议静态变量是 dll 的本地变量,而不是导出的。

以下讨论摘要

调用应用程序中的代码不能直接访问静态成员,只能通过dll中类的成员函数访问。然而,有几个内联函数访问静态成员。这些函数将被内联扩展到调用应用程序代码中,使调用应用程序直接访问静态成员。这将违反上面提到的发现,即静态变量是 dll 的本地变量,不能从调用应用程序中引用。

于 2010-03-19T19:08:56.600 回答
16

我的猜测是使用 DLL 的类应该在标题中看到dllimport而不是dllexport 。如果我是正确的,这通常可以通过定义一个预处理器宏来实现,例如:

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

然后在类声明中使用它:

class DECLSPEC Test{
protected:
    static int d;
public:
    static void m(){}
}

因此,在 Test.cpp(或在您的 DLL 项目中有意义的任何地方)中,您可以指定要导出,以便使用dllexport导出:

#define EXPORTING
#include "Test.h"

int Test::d;

而另一个未定义 EXPORTING 的项目将看到dllimport

是否有意义?

于 2010-03-19T19:06:08.747 回答
5

__declspec(dllexport)对于 Windows DLL, vs之间有一个特定的区别__declspec(dllimport)dllexport应该在编译 DLL 时dllimport使用,应该在编译链接到该 DLL 的程序时使用。定义它的标准方法是使用宏。

以下是视觉工作室示例:

// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the DLL_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see 
// DLL_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef DLL_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
于 2010-03-19T19:26:41.887 回答
0

尽管有总结,但可以从 DLL 导出静态数据。但是,Visual Studio DLL 项目提供的标准宏存在一个问题:

#ifdef DLL_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif

如果您有多个 DLL 从一个 DLL 调用代码到另一个 DLL 或在 EXE 和 DLL 之间调用代码,那么这个宏就会出现问题,因为每个头文件都将被导出。需要处理 __declspec 的独特宏。处理此问题的最安全方法如下:

#ifdef MYPROJECT_DLL_EXPORTS
     #define MYPROJECT_API __declspec(dllexport)
#else
     #define MYPROJECT_API __declspec(dllimport)
#endif

然后只在 DLL 项目的编译器预处理器选项中定义 MYPROJECT_API。在您的标头代码中:

struct/class MYPROJECT_API myclass {
   static int counter;
};

在 .cpp 文件中:

int myclass::counter = 0;
于 2021-03-12T17:09:19.920 回答
-2

对 c++17 使用内联 static inline std::string static_variable

于 2021-06-30T23:22:13.750 回答