小编tdd*_*tdd的帖子

访问使用def文件导出的静态变量时崩溃

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

library.h

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

struct B {
  static int b;
  static int get();
};
Run Code Online (Sandbox Code Playgroud)

library.cpp

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

int B::b = 0;
int B::get() {return b;}
Run Code Online (Sandbox Code Playgroud)

library.def

LIBRARY

EXPORTS
  ?b@B@@2HA
  ?get@B@@SAHXZ
Run Code Online (Sandbox Code Playgroud)

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 …
Run Code Online (Sandbox Code Playgroud)

c++ dll visual-c++

5
推荐指数
1
解决办法
112
查看次数

标签 统计

c++ ×1

dll ×1

visual-c++ ×1