简体   繁体   English

围绕可以由 C 链接器链接的 C++ 库创建 C 包装器

[英]Create a C wrapper around a C++ library that can be linked by a C linker

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.按照对这个问题的回答(为面向对象的 C++ 代码开发 C 包装器 API),我设法为我的 C++ 代码编写了一个 C 包装器。

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++).我想将我的包装器编译并链接到一个静态库(使用 g++ 编译),该库只能使用 gcc(而不是 g++)使用、编译和链接。 This way the user of the library would not have to care that the library is written in C++.这样,库的用户就不必关心库是用 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:这个链接解释了一些编译器选项和场景:http ://docs.oracle.com/cd/E19422-01/819-3690/Building.Libs.html具体来说:

> 16.7 Building a Library That Has a C API > 16.7 构建具有 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).如果要构建用 C++ 编写但可与 C 程序一起使用的库,则必须创建 C API(应用程序编程接口)。 To do this, make all the exported functions extern "C".为此,将所有导出的函数设置为 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.如果 C 接口库需要 C++ 运行时支持并且您正在使用 cc 进行链接,那么您在使用 C 接口库时还必须将您的应用程序与 libC(兼容模式)或 libCrun(标准模式)链接。 (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. (如果 C 接口库不需要 C++ 运行时支持,则您不必与 libC 或 libCrun 链接。)归档库和共享库的链接步骤不同。

When providing an archived C-interface library, you must provide instructions on how to use the library.提供存档的 C 接口库时,您必须提供有关如何使用该库的说明。

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.如果 C 接口库是在标准模式(默认)下使用 CC 构建的,则在使用 C 接口库时将 -lCrun 添加到 cc 命令行。 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.如果 C 接口库是在兼容模式 (-compat) 下使用 CC 构建的,则在使用 C 接口库时将 -lC 添加到 cc 命令行。 When providing a shared C-interface library you must create a dependency on libC or libCrun at the time that you build the library.提供共享 C 接口库时,您必须在构建库时创建对 libC 或 libCrun 的依赖项。 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.当共享库具有正确的依赖关系时,您在使用库时无需在命令行中添加 -lC 或 -lCrun。

If you are building the C-interface library in compatibility mode (-compat), add -lC to the CC command line when you build the library.如果您正在以兼容模式 (-compat) 构建 C 接口库,请在构建库时将 -lC 添加到 CC 命令行。 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.如果您在标准模式(默认)下构建 C 接口库,请在构建库时将 -lCrun 添加到 CC 命令行。 If you want to remove any dependency on the C++ runtime libraries, you should enforce the following coding rules in your library sources:如果要删除对 C++ 运行时库的任何依赖,则应在库源中强制执行以下编码规则:

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).不要使用运行时类型信息 (RTTI)。

Yes, you just need to provide the C interface with functions that have C linkage.是的,您只需要为 C 接口提供具有 C 链接的函数。 Exactly as the linked question's answer work although for the header you will need to make it C-compliant.与链接问题的答案完全一样,尽管对于标题,您需要使其符合 C。 The common way would be using an #ifdef __cplusplus to detect whether the compiler is a C or C++ compiler.常用的方法是使用#ifdef __cplusplus来检测编译器是 C 还是 C++ 编译器。

// MyHeader
#ifndef MYHEADER
#define MYHEADER
#ifdef __cplusplus
// Class definition or any other C++ code
extern "C" {
#endif
// C only code here
#ifdef __cplusplus
}
#endif
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM