5

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?

Community
  • 1
  • 1
Mathieu Pagé
  • 10,764
  • 13
  • 48
  • 71
  • Following link explains a bit: http://docs.oracle.com/cd/E19422-01/819-3690/Building.Libs.html Scroll down to 16.7 for compiler options which I think apply to your situation – Neil Neyman Aug 12 '13 at 20:29
  • 1
    @NeilNeyman This answer my question indeed. Unfortunately it confirms the client application of my library needs to be linked with stdc++. But it's seems that's the only way to go. If you make this an answer I'll accept it. – Mathieu Pagé Aug 13 '13 at 03:35

2 Answers2

2

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).

Mathieu Pagé
  • 10,764
  • 13
  • 48
  • 71
Neil Neyman
  • 2,116
  • 16
  • 21
1

Yes, you just need to provide the C interface with functions that have C linkage. Exactly as the linked question's answer work although for the header you will need to make it C-compliant. The common way would be using an #ifdef __cplusplus to detect whether 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
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • That's pretty much what I did. Then using g++ and ar I compiled this to wrapper.a. When I write a C application and try to link it with wrapper.a it has some undefined references (to things like std::string stuff for example). If I use g++ (which I want to avoid) it works. – Mathieu Pagé Aug 12 '13 at 20:31
  • @MathieuPagé: Well, you will have to link all the object files you need, and probably the C++ library, either statically or dynamically – David Rodríguez - dribeas Aug 12 '13 at 20:32
  • 1
    Link C++ library statically, if you want to avoid having the client application have to provide `std::string` or the like – Yakk - Adam Nevraumont Aug 12 '13 at 20:37