【问题标题】:Crash when accessing static variable exported with a def file访问使用 def 文件导出的静态变量时崩溃
【发布时间】:2019-06-30 04:16:29
【问题描述】:

我正在使用 def 文件从 dll 中导出一些静态函数和变量。在导入 dll 后访问静态变量时,程序崩溃。任何想法为什么会发生这种情况?我正在使用 VS2017,Windows SDK 10.0.17763.0。

图书馆.h

struct DLLEXPORT A {
  static int a;
  static int get();
};

struct B {
  static int b;
  static int get();
};

库.cpp

int A::a = 0; 
int A::get() {return a;}

int B::b = 0;
int B::get() {return b;}

库.def

LIBRARY

EXPORTS
  ?b@B@@2HA
  ?get@B@@SAHXZ

main.cpp

int main() {
  int a = A::get(); // Works fine
  int b = B::get(); // Works fine

  A::a = 1; // Works fine
  B::b = 1; // CRASH (Access violation writing location ...)
  return 0;
}

【问题讨论】:

  • 我认为您还需要导出 B::b;
  • @SHR 是在def文件中导出的,不是吗?否则 OP 会得到未解决的外部符号链接器错误。

标签: c++ visual-c++ dll


【解决方案1】:

我认为 def 文件条目缺少 DATA 属性,因此 B::b 被视为通常只读的代码:

?b@B@@2HA DATA

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 2017-11-01
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多