first:定义导出符号
ExportDef.h
#ifndef ExportDef_H //防止.h文件被包含多次,等同#pragma once,#pragma once不能跨平台
#define ExportDef_H
#if defined(_WIN32)
#if defined(DEMO_EXPORTS) //为该导出库定义预定义宏,需添加到C/C++的预处理器定义中去
#define DEMO_API __declspec(dllexport)
#else
#define DEMO_API __declspec(dllimport)
#endif
#else
#if defined(__GNUC__)
#define DEMO_API __attribute__((visibility("default")))
#endif
#endif
//#ifdef _WIN32
//#ifdef DEMO_EXPORTS
//#define DEMO_API __declspec(dllexport)
//#else
//#define DEMO_API __declspec(dllimport)
//#endif
//#else
//#ifdef __GNUC__
//#define DEMO_API __attribute__((visibility("default")))
//#endif
//#endif
#endif //!ExportDef_H
second: 声明导出函数
IDemo.h
#ifndef IDEMO_H
#define IDEMO_H
#include "ExportDef.h"
#ifdef __cplusplus
extern "C"{
#endif
/*!
* 浮点数相加
* @param prev [in] 前一个数
* @param next [in] 后一个数
*/
DEMO_API double add(double prev, double next);
#ifdef __cplusplus
}
#endif
#endif // !IDEMO_INCLUDE
next: 定义导出函数
IDemo.cpp
#include "IDemo.h"
DEMO_API double add(double prev, double next)
{
return prev + next;
}
end: 跨平台定义
windows
windows需要加入dllmain.cpp文件
dllmain.cpp : 定义 DLL 应用程序的入口点。
#ifdef _WIN32
#include <windows.h>
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#endif //_WIN32
linux
不需要定义入口函数