해석 VC에서 DLL을 만들고 전역 변수, 함수 및 클래스를 내보내는 심도 있는 분석

1791 단어
하나.DLL 생성
1. VC에서 Win32 빈 항목MathLib을 새로 만듭니다.2. 사전 컴파일 헤더 파일 stdafx를 추가합니다.h, 가져오기 내보내기 제어 기호를 정의합니다.
 
  
//stdafx.h
#pragma once
#define MATHLIB_EXPORT

3. 내보낼 전역 변수, 함수 및 클래스를 포함하는 헤더 파일MathLib을 추가합니다.h:
 
  
 //MathLib.h
 #pragma once

 #ifdef MATHLIB_EXPORT
 #define MATHLIBAPI __declspec(dllexport)
 #else
 #define MATHLIBAPI __declspec(dllimport)
 #endif

 //macro
 #define PI 3.14149

 //Global variable
 extern MATHLIBAPI int GlobalVariable;

 //Function
 MATHLIBAPI int Add(int a,int b);

 //Class
 class MATHLIBAPI Math
 {
 public:
  int Multiply(int a,int b);
 };

4. 내보낸 요소의 구현 파일인 MathLib을 추가합니다.cpp
 
  
 //MathLib.cpp
 #include "stdafx.h"
 #include "MathLib.h"

 int GlobalVariable = 100;

 int Add(int a,int b)
 {
  return a+b;
 }

 int Math::Multiply(int a,int b)
 {
  return a*b;
 }

둘째, 생성된 DLL 테스트 코드를 테스트합니다.
 
  
 #include "stdafx.h"
 #include
 using namespace std;

 #include "../MathLib/MathLib.h"
 #pragma comment(lib,"../Debug/MathLib.lib")

 int _tmain(int argc, _TCHAR* argv[])
 {
  cout<
  cout<
  int a = 20,b = 30;
  cout<  cout<
  Math math;
  cout<
  return 0;
 }

좋은 웹페이지 즐겨찾기