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