围绕可以由 C 链接器链接的 C++ 库创建 C 包装器 [英] Create a C wrapper around a C++ library that can be linked by a C linker

查看:34
本文介绍了围绕可以由 C 链接器链接的 C++ 库创建 C 包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照 这个问题的答案(开发 C 包装器面向对象的 C++ 代码的 API) 我设法为我的 C++ 代码编写了一个 C 包装器.

我想将我的包装器编译并链接到一个静态库(使用 g++ 编译),该库只能使用 gcc(而不是 g++)使用、编译和链接.这样库的用户就不必关心库是用 C++ 编写的.

这可能吗?

解决方案

此链接解释了一些编译器选项和场景:http://docs.oracle.com/cd/E19422-01/819-3690/Building.Libs.html 具体:

> 16.7 构建具有 C API 的库

<块引用><块引用>

如果您想构建一个用 C++ 编写的库,但可以与 C 程序一起使用,您必须创建一个 C API(应用程序编程接口).为此,请制作所有导出的函数外部C".请注意,这只能用于全局函数和不适用于成员函数.

如果 C 接口库需要 C++ 运行时支持,而您正在与 cc 链接,那么您还必须将您的应用程序与libC(兼容模式)或 libCrun(标准模式),当您使用C 接口库.(如果 C 接口库不需要 C++运行时支持,那么您不必与 libC 或 libCrun 链接.)归档库和共享库的链接步骤不同.

在提供存档的 C 接口库时,您必须提供有关如何使用该库的说明.

如果 C 接口库是在标准模式下使用 CC 构建的(默认),使用 C 接口时在 cc 命令行中添加 -lCrun图书馆.如果 C 接口库是与 CC 兼容构建的模式(-compat),使用时在 cc 命令行中添加 -lCC 接口库.在提供共享 C 接口库时,您必须在构建时创建对 libC 或 libCrun 的依赖项图书馆.当共享库具有正确的依赖关系时,您使用时不需要在命令行中添加 -lC 或 -lCrun图书馆.

如果您在兼容模式下构建 C 接口库(-compat),在构建库时将 -lC 添加到 CC 命令行.如果您在标准模式下构建 C 接口库(默认),在构建时将 -lCrun 添加到 CC 命令行图书馆.如果要删除对 C++ 运行时的任何依赖库,您应该在您的图书馆来源:

除非您自己提供,否则请勿使用任何形式的 new 或 delete对应的版本.不要使用异常.不要使用运行时类型信息 (RTTI).

Following the answer given to this question (Developing C wrapper API for Object-Oriented C++ code) I managed to write a C wrapper for my C++ code.

I would like to compile and link my wrapper into a static library (compiled using g++) that could be used, compiled and linked using gcc only (not g++). This way the user of the library would not have to care that the library is written in C++.

Is this something possible?

解决方案

This link explains some of the compiler options and scenarios: http://docs.oracle.com/cd/E19422-01/819-3690/Building.Libs.html Specifically:

> 16.7 Building a Library That Has a C API

If you want to build a library that is written in C++ but that can be used with a C program, you must create a C API (application programming interface). To do this, make all the exported functions extern "C". Note that this can be done only for global functions and not for member functions.

If a C-interface library needs C++ run-time support and you are linking with cc, then you must also link your application with either libC (compatibility mode) or libCrun (standard mode) when you use the C-interface library. (If the C-interface library does not need C++ run-time support, then you do not have to link with libC or libCrun.) The steps for linking differ for archived and shared libraries.

When providing an archived C-interface library, you must provide instructions on how to use the library.

If the C-interface library was built with CC in standard mode (the default), add -lCrun to the cc command line when using the C-interface library. If the C-interface library was built with CC in compatibility mode (-compat), add -lC to the cc command line when using the C-interface library. When providing a shared C-interface library you must create a dependency on libC or libCrun at the time that you build the library. When the shared library has the correct dependency, you do not need to add -lC or -lCrun to the command line when you use the library.

If you are building the C-interface library in compatibility mode (-compat), add -lC to the CC command line when you build the library. If you are building the C-interface library in standard mode (the default), add -lCrun to the CC command line when you build the library. If you want to remove any dependency on the C++ runtime libraries, you should enforce the following coding rules in your library sources:

Do not use any form of new or delete unless you provide your own corresponding versions. Do not use exceptions. Do not use runtime type information (RTTI).

这篇关于围绕可以由 C 链接器链接的 C++ 库创建 C 包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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