Lib库
LibA.h
#pragma once class LibA { public: LibA(void); ~LibA(void); void show(); };
LibA.cpp
#include "StdAfx.h" #include "LibA.h" #include <iostream> using namespace std; LibA::LibA(void) { } LibA::~LibA(void) { } void LibA::show() { cout<<"LibA show"<<endl; }
DLL库(静态引用)
dll.h
#ifndef _DLL_H_ #define _DLL_H_ #ifdef BUILDING_DLL # define DLLIMPORT __declspec (dllexport) #else /* Not BUILDING_DLL */ # define DLLIMPORT __declspec (dllimport) #endif /* Not BUILDING_DLL */ DLLIMPORT void FuncA(void); class DLLIMPORT DllClass { public: DllClass(); virtual ~DllClass(void); void show(); private: }; #endif /* _DLL_H_ */
dllmain.cpp
# define BUILDING_DLL #include "dll.h" #include <windows.h> #include <iostream> using namespace std; DllClass::DllClass() { } DllClass::~DllClass () { } void DllClass::show() { cout<<" Class DLLClass show"<<endl; } DLLIMPORT void FuncA(void) { cout<<"FuncA show"<<endl; } BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ , DWORD reason /* Reason this function is being called. */ , LPVOID reserved /* Not used. */ ) { switch (reason) { case DLL_PROCESS_ATTACH: break; case DLL_PROCESS_DETACH: break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: break; } /* Returns TRUE on success, FALSE on failure */ return TRUE; }
控制台引用代码
Tmain.cpp
#include "stdafx.h" #include "LibA.h" #include "dll.h" int _tmain(int argc, _TCHAR* argv[]) { LibA liba; liba.show(); DllClass dll; dll.show(); FuncA(); return 0; }