本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – 导出接口函数和导出C++类
原文链接:https://www.stubbornhuang.com/2497/
发布于:2023年02月03日 11:21:05
修改于:2023年02月03日 11:21:05

在C++实际的项目开发过程中,经常需要将核心功能打包成动态链接库(Dynamic Link Library)供第三方调用,在dll中我们可以不仅可以导出接口函数还可以导出C++类。
下面以简单的示例代码说明如何在C++导出接口函数和类。
1 C++中导出接口函数和导出C++类
1.1 C++动态链接库中的导出代码
在C++的动态链接库中,__declspec(dllexport)
用于导出dll中的成员,而__declspec(dllimport)
用于导入外部dll中的成员。
所以如果需要导出接口函数和类,需要在dll的代码中预先定义__declspec(dllexport)
,并将__declspec(dllexport)
放在需要导出的接口函数和类之前。
在linux系统中,__declspec(dllexport)
不可用,具有相同功能的是__attribute__((visibility ("default")))
。
1.2 跨平台导出宏的写法
由于Windows和Linux系统的导出代码的不一致性,我们需要对上述的接口进行跨平台封装,示例代码如下
#define EXPORT
/* 定义动态链接库dll的导出 */
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#ifdef EXPORT
#define EXPORT_API __declspec(dllexport)
#else
#define EXPORT_API __declspec(dllimport)
#endif
#elif defined(linux) || defined(__linux)
#include <stdlib.h>
#ifdef EXPORT
#define EXPORT_API __attribute__((visibility ("default")))
#else
#endif
#endif
仔细查看上述代码,我们使用EXPORT_API
作为统一的导出宏。
1.3 导出接口函数
我们可以使用1.2节中的导出宏EXPORT_API
来导出接口函数,这里以简单的加法函数为例
export_function.h
#ifndef EXPORT_FUNCTION_H_
#define EXPORT_FUNCTION_H_
#define EXPORT
/* 定义动态链接库dll的导出 */
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#ifdef EXPORT
#define EXPORT_API __declspec(dllexport)
#else
#define EXPORT_API __declspec(dllimport)
#endif
#elif defined(linux) || defined(__linux)
#include <stdlib.h>
#ifdef EXPORT
#define EXPORT_API __attribute__((visibility ("default")))
#else
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifndef EXPORT_API
#define EXPORT_API
#endif
EXPORT_API int Add(int a, int b);
#ifdef __cplusplus
}
#endif
#endif // !EXPORT_FUNCTION_H_
export_function.cpp
#include "export_function.h"
EXPORT_API int Add(int a, int b)
{
return a + b;
}
1.4 导出C++类
我们可以使用1.2节中的导出宏EXPORT_API
来导出C++类,下面看一个简单的示例
export_class.h
#ifndef _EXPORT_CLASS_H_
#define _EXPORT_CLASS_H_
#define EXPORT
/* 定义动态链接库dll的导出 */
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#ifdef EXPORT
#define EXPORT_API __declspec(dllexport)
#else
#define EXPORT_API __declspec(dllimport)
#endif
#elif defined(linux) || defined(__linux)
#include <stdlib.h>
#ifdef EXPORT
#define EXPORT_API __attribute__((visibility ("default")))
#else
#endif
#endif
class EXPORT_API Example
{
public:
Example();
Example(int a, int b);
public:
int GetAddResult();
public:
int m_A;
int m_B;
};
#endif // !_EXPORT_CLASS_H_
export_class.cpp
#include "export_class.h"
Example::Example()
:m_A(0),
m_B(0)
{
}
Example::Example(int a, int b)
:m_A(a),
m_B(b)
{
}
int Example::GetAddResult()
{
return m_A + m_B;
}
当前分类随机文章推荐
- C++ - return this和return *this的含义和区别 阅读273次,点赞0次
- C++ – UTF8编码下的全角字符转半角字符 阅读1580次,点赞0次
- C++ - 判断本机文件是否存在的方式总结 阅读4556次,点赞0次
- C++11 - std::function简要介绍以及可包装函数的几种形式总结 阅读3054次,点赞0次
- C++ - C++使用cuda api获取当前GPU显卡的总共的显存容量、已使用显存容量、剩余显存容量 阅读3867次,点赞2次
- C++ - 拷贝构造函数与拷贝构造函数调用时机 阅读261次,点赞0次
- C++11 - 使用std::thread在类内部以成员函数作为多线程函数执行异步操作 阅读2257次,点赞0次
- C++ - vector存储动态指针时正确释放内存 阅读5580次,点赞0次
- C++ - std::string字符串格式化方法总结 阅读653次,点赞0次
- C++11/std::condition_variable - 生产者消费者模型 阅读2720次,点赞0次
全站随机文章推荐
- 资源分享 - Game Programming Gems 2 英文高清PDF下载 阅读1847次,点赞0次
- C++11/std::thread - 线程管理join/detach 阅读2248次,点赞0次
- WordPress - get_header函数,加载主题头部header模板 阅读867次,点赞0次
- 资源分享 - Essential Mathematics for Games and Interactive Applications(Third Edition) 英文高清PDF下载 阅读1832次,点赞0次
- C++ – 字节数组byte[]或者unsigned char[]与long double的相互转换 阅读916次,点赞0次
- MindSpore - LeNet5的MindSpore实现 阅读936次,点赞0次
- 网站推荐 - 注册chatGPT无法接收验证短信?使用便宜好用的sms-activate 阅读4081次,点赞2次
- C++11/std::thread - 可作为线程函数的几种方式总结 阅读3472次,点赞1次
- TortoiseGit - 本地仓库更改远程仓库URL 阅读654次,点赞0次
- Alphapose - Windows下Alphapose(Pytorch 1.1+)版本2021最新环境配置步骤以及踩坑说明 阅读4694次,点赞1次
评论
167