Create a C wrapper around the C ++ library, which may be associated with the C-linker

In response to this question (developing a wrapper API for object-oriented C ++ code), I was able to write a C shell for my C ++ code.

I would like to compile and link my wrapper in a static library (compiled using g ++) that can be used, compiled and linked using only gcc (and not g ++). Thus, the library user does not have to make sure that the library is written in C ++.

Is it possible?

+4
source share
2 answers

This link describes some parameters and scripts of the compiler: http://docs.oracle.com/cd/E19422-01/819-3690/Building.Libs.html In particular:

> 16.7 Building a library with the API API

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

If the C-interface library requires C ++ runtime support, and you are linking to cc, then you should also link your application with libC (compatibility mode) or libCrun (standard mode) when you use the C-interface library. (If the C interface library does not require C ++ runtime support, then you do not need to contact libC or libCrun.) The binding steps are different for archived and shared libraries.

When providing the C-interface archive library, you must provide instructions for using the library.

If the C-interface library was built with CC in standard mode (default), add -lCrun at the cc command line when using the C-interface library. If the C-interface library was built with CC in mode (-compat) compatibility, add -lC to the cc command line when using the C-Interface Library. By providing a common C-interface library, you must create a dependency on libC or libCrun at the time the library is created. 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 create the C-interface library in compatibility mode (-compat), add -lC to the CC command line when building the library. If you are building the C-interface library in standard mode (default), add -lCrun at the CC command line when creating the library. If you want to remove any dependency on the runtime of C ++ libraries, you must apply the following encoding rules in your library sources:

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

+1
source

Yes, you just need to provide a C interface with functions that have a C link. Just as the answer to a related question works, although for the title you need to make it C-compatible. A common way would be to use #ifdef __cplusplus to determine if the compiler is a C or C ++ compiler.

 // 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 
+1
source

Source: https://habr.com/ru/post/1496669/


All Articles