相关疑难解决方法(0)

C++如何从dll导出静态类成员?

// API mathAPI.h,在Dll.cpp和Test.cpp中

#ifdef __APIBUILD
#define __API __declspec(dllexport)
//#error __APIBUILD cannot be defined.
#else
#define __API __declspec(dllimport)
#endif

class math
{
 public:
   static __API double Pi;
   static __API double Sum(double x, double y);
};
Run Code Online (Sandbox Code Playgroud)

//定义了Dll.cpp __APIBUILD

#include "mathAPI.h"

double math::Pi = 3.14;

double math::Sum(double x, double y)
{
  return x + y;
}
Run Code Online (Sandbox Code Playgroud)

// Test.cpp __APIBUILD未定义

#include <iostream>
#pragma comment(lib, "dll.lib")
#include "mathAPI.h"

int main()
{
  std::cout << math::Pi; //linker error
  std::cout << math::Sum(5.5, 5.5); //works fine
  return 0;
} …
Run Code Online (Sandbox Code Playgroud)

c++ windows dll

8
推荐指数
1
解决办法
4029
查看次数

标签 统计

c++ ×1

dll ×1

windows ×1