dll跨平台导出

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

不需要定义入口函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值