运送仅带有C api的C ++ dll [英] Shipping a C++ dll with only a C api exposed

查看:68
本文介绍了运送仅带有C api的C ++ dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个软件,其API旨在仅用于C语言,因为它很容易将C代码与其他软件/客户端链接。

I'm writing a piece of software whose API is intended to be C only , because it is easy to link C code against other softwares/clients.

但是,实际的程序代码是使用所有常见的C ++功能(如异常,STL等)在C ++中完成的。

The actual program code however is done in C++, using all the usual C++ features like exception, STL, etc.

导出的API /标头本身将用纯C编写,使用 export C 关键字。

The exported API / headers themselves will be written in pure C, with the export "C" keywords.

如果我打算将此dll交付给我,应该注意什么?不了解C ++的用户?通常,他们不必担心实际代码是C ++的事实,只需要知道如何通过头文件链接到C代码即可。
有人告诉我,我应该确保libstd是静态链接的,在所有平台上都不可能这样(会有linux和Windows构建)。例外情况如何?等等

What should I be wary of if I intend to deliver this dll to users who have no knowledge of C++ on their side? Normally they should not be concerned about the fact that the actual code is in C++, and only be required to know how to link against C code via a header file. I've been told that I should ensure that libstd is linked statically, which may be not be possible on all platforms (there will be a linux and a windows build). How about exceptions ? etc

推荐答案

1)将标头换成:

#ifdef __cplusplus
extern "C" {
#endif

2)创建不透明的指针:

2) make opaque pointers:

struct myInternalStructFOO; // incomplete type
typedef struct myInternalStructFOO *cFOO; // public type

class myInternalClassBAR; // incomplete type
typedef class myInternalClassBAR *cBAR; // public type

您无需在公共api中提供类定义。
类定义将包含在某些私有标头中。

you need not to give class definitions in your public api. class definitions will go in some private headers.

这篇关于运送仅带有C api的C ++ dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆