Exposing a C++ library via C bindings [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Are there any available guidelines, habits, best practices... on how to expose a C++ library via C bindings (compiled as part of the library).
The objective is to not modify the C++ classes.
Assuming the following two C++ classes
class B {
public:
std::vector<int> list();
std::string toString();
};
class A {
public:
explicit A(size_t size);
B getB();
};
What would be the "best" way to expose these as a C API; a list of functions handling pointers and instantiating objects on the heap, callbacks ? Is there a list of generally accepted naming conventions ?
extern "C" {
A* A_new(size_t size);
void A_delete(A* a);
B* A_getB(A *a);
void B_delete(B* b);
...
}
(note: asking for generally accepted practices and conventions and/or tools is not asking for opinions but practical feedback)

My experience is that the basic question to answer is whether the C++ objects are singletons of some kind or if there will be multiple instances of these objects on the C side of the interface.
If they are singletons then the C interface can be a set of functions and all of the management, object handles, and other bits are held within the C++ side of the interface. The user of the library doesn't need to have any kind of a way to reference a particular C++ object.
However if there are multiple instances of the C++ objects then the decision as to how to handle these multiple objects and their lifetime has to be determined. The main two ways I have seen are (1) providing a pointer to the C++ object over to the C side of the interface or (2) providing a handle to the C++ object over to the C side of the interface with the handle referencing a management area on the C++ side.
I personally like the handle method because then modern C++ practices can be used for managing the lifetime of the objects. The objects are stored in some C++ container with a unique handle or identifier provided to the C interface and the C++ side owns the objects. The C side is requesting specific operations on the objects through the C interface.
The C interface may vary between being unique functions that map to the individual C++ object methods or being a more generic function that takes what amounts to a command flag by specifying a unique identifier for the C++ object method.
In the first alternative the methods of the C++ object are exposed as a C interface function which, in the case of multiple instances, takes either a pointer to the C++ object or the handle to the object. The C interface function, written in C++, then does the actual C++ object method call with the proper arguments. So this is just a C interface function that forwards the parameters to the C++ object.
In the second alternative, the C++ interface function has to determine, using a lookup table or a switch or something similar which method to invoke. Parameters may either be specified in the interface or variable arguments may be used. If variable arguments are used then you have the problem of parsing out the parameter list, marshaling the arguments, and then calling the C++ object method.
An additional consideration is the naming and namespace clutter. In C there is the traditional three letter prefix approach to naming of externals though in most cases more characters are more meaningful.
However the approach I really prefer is to have an external struct within which are function pointers to the C functions making up the interface. By doing this, you can name the struct something meaningful for your library and then you are free to name the actual function pointers however you like.
The actual C interface functions are static within the source file which exposes only the struct containing the function pointers. The struct itself is a global that is initialized when it is defined at the bottom of the source file containing the C interface functions.

What you're proposing is typically the best way to handle this. You have a function which return a pointer to a newly allocated object, a set of function that take that pointer and do various things, and one more that takes the pointer and frees the memory.
The only thing you need to add is a declaration for the type that is compatible with C:
typedef struct A A;
typedef struct A B;

You shouldn't expose std types in your C interface (public members in class B) because the C++ ABI is not standardized and you might end up with bugs that are very hard to figure out when the API consumer is using a different compiler version.
For example in your code I would rewrite B as follows:
class B {
public:
int* list(); // or write your own implementation for a vector type which you ship with the api
const char* toString();
};

Related

For what purpose non-virtual method will be used with C++? [duplicate]

This question already has answers here:
Is there any reason not to make a member function virtual?
(7 answers)
Closed 4 years ago.
What will be the purpose of using non-virtual method in terms of functionality with C++? Is there any merit to change a object's method by its object handle rather than its object type? I found many articles about how virtual method will be utilized but I could not find how non-virtual method will be utilized.
Other languages I know of such as Java, Ruby and Python only have virtual method. So functionally non-virtual method is not needed and just be used for performance reason?
OK. I hadn't read the article marked duplicate of. But your answers are still valuable to me in the point the telling the origin of C++ and comparing C++ to other object oriented languages. Thanks everyone to answer.
The answer is very simple : because C++ is not Java.
Programming languages have different philosophies and different ways to accomplish the same result.
Java (and other "OOP-language-where-every-object-is-GCed-and-is-a-reference-type", like C#) encourage you to think about objects in a very specific way: Inheritance and Polymoprphism are the main ways to achieve flexibility and generalization of code. Objects are almost always reference type, meaning that Car car can actually point to Toyota, Ford and whatever. objects are garbaged-collected and dynamically allocated. All objects anyway inherit from an Object class, so inheritance and dynamic polymorphism is anyway imbued into the language objects by the very language design.
C++ is different. the concept of object might be central to the language, but an object is basically a unit of data and functionality. it's a leaner form of a "real" OOP-language object and it usually allocated on the stack, uses RAII to handle its own resources, and is a value type.
Inheritance and Polymorphism exist, but they are inferior to composition and compile-time-polymorphism (templates).
C++ doesn't encourage you to think about objects as a reference type. objects might be a reference type, they might have virtual functions, but this is only one way to achieve flexibility and generalization in C++, as opposed to Java. you might use templates instead, function pointers and opaque types (a-la C style polymorphism), inheritance + overriding function (a-la Java style), Hence, C++ doesn't force you to take the Java route to flexibility - it gives you the opportunity to choose the best way to accomplish things.
When marking a method as virtual, every time such method will be called, the program will have to check the virtual table inside the object that you're calling the method on, it's called dynamic dispatch. It creates quit a bit of overhead compared to normal methods that are resolved using static dispatch.
As big part of C++ is giving the programmer the choice what he wants to do, you can choose if you want static of dynamic linking.
C++ method lookup mechanisms won't allow for polymorphism if it's non-virtual. Defining classes as non-virtual will prevent the overhead and confusion.
Have a look at This question and answers
If a method is not virtual, the compiler knows the address in memory where this method's code will be located right at compile time, and can use it right away. If a method is virtual, it will have to be determined at runtime, which implementation should be called, based on the object type. It adds overhead on every call. So, by making a method non-virtual, you make it more efficient.
It should be mentioned that in some languages it's the other way around: the methods are "virtual" by default, but you can explicitly mark them as "non-virtual" (usually called final).
Non-virtual methods can add additional functionalities specific only to the derived class.
class animal {
public:
virtual string name() = 0;
};
class rhino :public animal {
public:
string name() override { return "Rhino"; }
int getHornSize() { return 10; } // non-virtual method add functionality only specific to rhino class
};

C++ to C conversion workflow [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
First thing, I am not looking for a tool that is going to produce an unmaintainable C code from an existing cpp code-base. It is a requirement from a client, so I cannot just use the CPP code base itself.
I am trying to work out a work-flow, so that I can convert the code-base from CPP to C incrementally without breaking the code. I have thought out a few things using the "extern C" approach
Classes to Structs
Member functions of the Classes will be converted to Struct_name_FunctionName format.
In case of a function being re-used by 2 classes, I plan to use function pointers.
Replace overloaded operators like +,- etc with actual functions. Eg a Add_, Sub_ etc
Can you folks add anymore ?
and point out potential pit-falls ?
I cannot share the code-base because of NDA.
The code base isn't huge in itself. Its got around 50 cpp files, saw some 100 odd classes.
Thanks in advance.
P.s I have gone through some other questions that sound similar. But they don't really have the work-flow that I am looking for.
[Closing] : Although the post has been put on hold because it is too broad, many of the comments were in fact useful. I am clearer on what needs to be done than before. Thank you.
Well, there's a very long list you'll have to tackle. Without claiming to be complete, you also need:
Virtual function support (not impossibly hard, function pointer table)
Constructors and destructors (Reasonable mapping to ordinary functions, lot of work)
Exceptions and stack unwinding (extremely hard)
Missing C++ library functions and classes (lot of work)
But don't kid yourself. std::map<std::string, std::pair<int, int>> is a very straightforward class that stores two ints for each string. The C translation of that class is an outright mess. MyMap["five,three"] = std::make_pair(5,3) can easily become 100+ lines of C code.
If your C++ code makes extensive use of OO constructs - particularly inheritance and polymorphism - you might wish to look at some C libraries that mimic this in C. qobject (from the qemu source tree) is one I know well, but better known (to nearly everyone bar me) is http://en.wikipedia.org/wiki/GObject which comes from glib. Nowadays, that isn't tied up with GUI code.
The advantage here is that you can change the language in use without making too many changes to the program flow.
glib also provides a fair number of other useful library constructs.
Following the advice to take the GObject as an example how C++-like code is done in C, there's one thing you can try out:
Translate the C++ code to Vala
Generate C code from Vala compiler.
Vala is a C#-like language; except that you'll have to repeat "public" or "private" in every function signature, as well as get rid of pointer-star in class types in use, there's no much things you'll have to do.
Of course, the generated C code will look the same ugly as from the other generators, but at least there are some short ways how to make it a "maintainable" code (the main problem is that every object created in Vala results in incrementing reference counter in C code, which isn't always necessary).
The GObject model can be a nice instruction/example as to how to translate C++ constructs into the same in C. If you don't use exceptions (Vala does! :) ), there should be no trouble with that. Of course the main guidelines are:
Normal methods: use the pointer to object as first argument. The function name is NAMESPACE_CLASSNAME_METHODNAME.
Virtual methods: you have to create a "characteristic object", pointed by the "object" of that class, which contains pointers to functions. The class's characteristic objects are usually created in GObject in functions that return pointer to that object - the object itself is lazily created and saved in a static local variable.
Overloading: add name-distinguising parts (Vala does not support overloading, even in constructors - constructors use special syntax for calling named constructors)
Templates: expand in place :)
Derivation: single derivation only (as in Vala as well), make the first field of the "derived class" structure the field of type of the "base class" structure.
Calling methods from base classes: use C cast. If you follow the point 5, you should simply cast the structure of derived class object to the structure of the base class.

C++ Class References

Coming from Delphi, I'm used to using class references (metaclasses) like this:
type
TClass = class of TForm;
var
x: TClass;
f: TForm;
begin
x := TForm;
f := x.Create();
f.ShowModal();
f.Free;
end;
Actually, every class X derived from TObject have a method called ClassType that returns a TClass that can be used to create instances of X.
Is there anything like that in C++?
Metaclasses do not exist in C++. Part of why is because metaclasses require virtual constructors and most-derived-to-base creation order, which are two things C++ does not have, but Delphi does.
However, in C++Builder specifically, there is limited support for Delphi metaclasses. The C++ compiler has a __classid() and __typeinfo() extension for retrieving a Delphi-compatible TMetaClass* pointer for any class derived from TObject. That pointer can be passed as-is to Delphi code (you can use Delphi .pas files in a C++Builder project).
The TApplication::CreateForm() method is implemented in Delphi and has a TMetaClass* parameter in C++ (despite its name, it can actually instantiate any class that derives from TComponent, if you do not mind the TApplication object being assigned as the Owner), for example:
TForm *f;
Application->CreateForm(__classid(TForm), &f);
f->ShowModal();
delete f;
Or you can write your own custom Delphi code if you need more control over the constructor call:
unit CreateAFormUnit;
interface
uses
Classes, Forms;
function CreateAForm(AClass: TFormClass; AOwner: TComponent): TForm;
implementation
function CreateAForm(AClass: TFormClass; AOwner: TComponent): TForm;
begin
Result := AClass.Create(AOwner);
end;
end.
#include "CreateAFormUnit.hpp"
TForm *f = CreateAForm(__classid(TForm), SomeOwner);
f->ShowModal();
delete f;
Apparently modern Delphi supports metaclasses in much the same way as original Smalltalk.
There is nothing like that in C++.
One main problem with emulating that feature in C++, having run-time dynamic assignment of values that represent type, and being able to create instances from such values, is that in C++ it's necessary to statically know the constructors of a type in order to instantiate.
Probably you can achieve much of the same high-level goal by using C++ static polymorphism, which includes function overloading and the template mechanism, instead of extreme runtime polymorphism with metaclasses.
However, one way to emulate the effect with C++, is to use cloneable exemplar-objects, and/or almost the same idea, polymorphic object factory objects. The former is quite unusual, the latter can be encountered now and then (mostly the difference is where the parameterization occurs: with the examplar-object it's that object's state, while with the object factory it's arguments to the creation function). Personally I would stay away from that, because C++ is designed for static typing, and this idea is about cajoling C++ into emulating a language with very different characteristics and programming style etc.
Type information does not exist at runtime with C++. (Except when enabling RTTI but it is still different than what you need)
A common idiom is to create a virtual clone() method that obviously clones the object which is usually in some prototypical state. It is similar to a constructor, but the concrete type is resolved at runtime.
class Object
{
public:
virtual Object* clone() const = 0;
};
If you don't mind spending some time examining foreign sources, you can take a look at how a project does it: https://github.com/rheit/zdoom/blob/master/src/dobjtype.h (note: this is a quite big and evolving source port of Doom, so be advised even just reading will take quite some time). Look at PClass and related types. I don't know what is done here exactly, but from my limited knowledge they construct a structure with necessary metatable for each class and use some preprocessor magic in form of defines for readability (or something else). Their approach allows seamlessly create usual C++ classes, but adds support for PClass::FindClass("SomeClass") to get the class reference and use that as needed, for example to create an instance of the class. It also can check inheritance, create new classes on the fly and replace classes by others, i. e. you can replace CDoesntWorksUnderWinXP by CWorksEverywhere (as an example, they use it differently of course). I had a quick research back then, their approach isn't exceptional, it was explained on some sites but since I had only so much interest I don't remember details.

Using c++ object in c

I am writing code in c++. I need to support few basic data types and something like BigInt. This types will be exposed to outside world(through header file), which might also include c program.
Should I implement BigInt like data type as struct or class?
The confusion is because
1. If I implement it as class, as OO advantages, I can do all processing in class. But I may have to implement some work around for c programs
2. If I implement it as struct I need not do anything special for c programs, but I loose modularity and ease of implementation.
basically C couldn't access C++ objects, either struct/class (they're the same in C++, only differs in default visibility specifier). You have to create procedural wrapper for the C++ object (i.e. creation, method call, destruction, etc).
For creation, create a function that returns opaque pointer (in C++, this would be the object pointer). For method call, add the returned pointer (from creation function above) as one of the (typically first) parameter. For destruction it's the same as method call, but typically receives no other parameter other than the pointer above.
If you plan on using it in C, I suggest you write it in C. C++ gets along with C a million times better than C gets along with C++. Another option would be to write it in C and then provide a thin C++ wrapper that gives it an OO interface.

A collection of custom structures (a wrapper) with a single member (also a custom structure), to a collection of the single members

The problem is specific but the solution open ended. I'm a lone coder looking to bat some ideas around with some fellow programmers.
I have a wrapper for a maths library. The wrapper provides the system with a consistent interface, while allowing me to switch in/out math libraries for different platforms. The wrapper contains a single member, so say for my Matrix4x4 wrapper class there is an api_matrix_4x4 structure as the only member to the wrapper.
My current target platform has a nifty little optimised library, with a few of those nifty functions requiring a C-style array of the wrapper's embedded member, while my wrapper functions for those math API functions don't want to expose that member type to the rest of the system. So we have a collection of wrappers (reference/pointer to) going into the function, & the members of the wappers being needed in a collection inside the function, so they can be passed to the math API.
I'm predominantly using C++, including C++11 features, & can also go C-style. Ideally I want a no-exception solution, & to avoid as many, if not all dynamic allocations. My wrapper functions can use standard library arrays or vectors, or C-style pointers to arrays as parameters, & whatever is necessary internally, just no dynamic casting (Run-Time Type Information).
1) Can I cast a custom struct/class containing a single custom struct, to the custom struct? If so, what about if it was a standard library collection of them. I'm thinking about type slicing here.
2) Would you perhaps use a template to mask the type passed to the function, although the implementation can only act on a single type (based on the math API used), or is such usage of templates considered bad?
3) Can you think of a nifty solution, perhaps involving swaps/move semantics/emplacement? If so, please help by telling me about it.
4) Or am I resigned to the obvious, iterate through one collection, taking the member out into another, then using that for the API function?
Example of what I am doing by the wrapper struct & wrapper function signature, & example of what I am trying to avoid doing is given by the function implementation:
struct Vector3dWrapper
{
API_Specific_Vector_3d m_api_vector_3d;
inline void operation_needing_vector_3d_wrappers(std::vector<Vector3d>& vectors)
{
// Now need a collection of API_Specific_Vector_3ds
try
{
std::Vector<API_Specific_Vector_3d> api_vectors;
api_vectors.reserve(vectors.size());
for( auto vectors_itr = vectors.begin(); vectors_itr != vectors.end(); ++vectors)
{
// fill each Vector3d.m_api_vector_3d into api_vectors
}
}
catch(std::bad_alloc &e)
{
// handle... though in reality, try/catch is done elsewhere in the system.
}
// Signature is API_Multiply_Vectors_With_Matrix_And_Project(API_Specific_Vector_3d* vectors, size_t vector_count)
API_Multiply_Vectors_With_Matrix_And_Project(&api_vectors, api_vectors.size());
}
};
You can cast a standard-layout struct (such as a struct compatible with C) to its first member, but what's the point? Just access the first member and apply &.
Templates usually allow uniform parameterization over a set of types. You can write a template that's only instantiated once, but again that seems pointless. What you really want is a different interface library for each platform. Perhaps templates could help define common code shared between them. Or you could do the same in plain C by setting typedefs before #include.
Solution to what? The default copy and move semantics should work for flat, C-style structs containing numbers. As for deep copies, if the underlying libraries have pointer-based structures, you need to be careful and implement all the semantics you'll need. Safe… simple… default… "nifty" sounds dirty.
Not sure I understand what you're doing with collections. You mean that every function requires its parameters to be first inserted into a generic container object? Constructing containers sounds expensive. Your functions should parallel the functions in the underlying libraries as well as possible.