建立dll項目后,在頭文件中,定義API宏
#ifndef API_S_H #define API_S_H ...... #ifndef DLL_S_20160424 #define API _declspec(dllimport) #else #define API _declspec(dllexport) #endif ......
int API apiFunction();
#endif
在頭文件導出函數前添加 API 類型說明
int API apiFunction();
關鍵的是.cpp中的宏定義
#include "stdafx.h" #define DLL_S_20160424 #include "api.h"
注意:DLL_S_20160424 的定義一定要在頭文件“api.h”之前,否則會出錯
error C2491: “apiFunction”: 不允許 dllimport 函數 的定義