// API mathAPI.h, cả trong Dll.cpp và Test.cppC++ Làm thế nào để xuất một thành viên lớp tĩnh từ một dll?
#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);
};
// Dll.cpp __APIBUILD được định nghĩa
#include "mathAPI.h"
double math::Pi = 3.14;
double math::Sum(double x, double y)
{
return x + y;
}
// Test.cpp __APIBUILD không được định nghĩa
#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;
}
lỗi 1 lỗi LNK2001: chưa được giải quyết biểu tượng bên ngoài "công cộng: static double Math :: Pi" (? Pi @ Math @@ 2na)
Làm cách nào để tôi làm việc này?
Có lỗi trong mã của bạn hoặc lỗi trong bài đăng của bạn vì 'Math :: Pi' khác với' math :: Pi' – nos