條件編譯
條件編譯常見用在定義動態鏈接庫時。
在源文件中定義為導出dllexport
在頭文件中定義為導入dllimport
這樣的頭文件既可以用在鏈接庫項目中,
也可以直接復制到引用項目中用。
Dll.h
#ifdef DLL_API
#else
#define DLL_API extern "C" _declspec(dllimport)
#endif
DLL_API int _stdcall add( int a, int b);
DLL_API int _stdcall subtract( int a, int b);
#else
#define DLL_API extern "C" _declspec(dllimport)
#endif
DLL_API int _stdcall add( int a, int b);
DLL_API int _stdcall subtract( int a, int b);
Dll.cpp
#define DLL_API extern "C" _declspec(dllexport)
#include " Dll.h "
#include <Windows.h>
#include <stdio.h>
int _stdcall add( int a, int b)
{
return a+b;
}
int _stdcall subtract( int a, int b)
{
return a-b;
}
#include " Dll.h "
#include <Windows.h>
#include <stdio.h>
int _stdcall add( int a, int b)
{
return a+b;
}
int _stdcall subtract( int a, int b)
{
return a-b;
}