How can I generate C wrappers over C++ API using SWIG? [duplicate] - c++

This question already has an answer here:
C++ to C Wrapper using SWIG (for FLTK)
(1 answer)
Closed 7 years ago.
I would like to generate C wrappers over some C++ API using SWIG.
From the SWIG documentation SWIG and C++:
6.2 Approach
To wrap C++, SWIG uses a layered approach to code generation. At the lowest level, SWIG
generates a collection of procedural ANSI-C style wrappers. These wrappers take care of
basic type conversion, type checking, error handling, and other low-level details of the
C++ >binding. These wrappers are also sufficient to bind C++ into any target language that
supports built-in procedures. In some sense, you might view this layer of wrapping as
providing a C library interface to C++.
However, I don't see any option to do just that, without generating code for using the API in a language like Python, Ruby, etc. How can I get just the C wrappers?

Although the main branch of SWIG cannot generate C wrappers for C++ API (the SWIG documentation quoted in the question is a bit misleading), there is a SWIG branch created during Google Summer of Code 2008 and available at https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd/ which does just that. I have found the answer here: C++ to C Wrapper using SWIG (for FLTK)

Related

SWIG C++ to {Dart/Flutter; and SWIFT} binding

I am looking for a nice way to have a C++ library exposed to Dart/Flutter and SWIFT (obviously targetting mobile here).
'been around SWIG for a long time, but can't find anything for new languages like Dart/Flutter & SFWIT (targetting Android/IOS dev).
We already have the Python and Java binding on SWIG, would be great to get those 2.
If SWIG can't do the trick, is there other wrappers of the like more appropriate?
There is no straight way to export C++ library to Dart/Flutter
Take advantage of dart:ffi (with package:ffi &
package:ffigen), to interact with C wrapper of C++
Use Fluttter plugin to bridge call from MethodChannel
to C++ library
Alternative (not tested!!!) use emscripten aka C++ -> asm.js then load it in flutter using the EmscriptenModule.
ref: https://pub.dev/documentation/web_ffi/latest/web_ffi_modules/EmscriptenModule-class.html
Also look at https://github.com/rwl/emscripten in the same mood.

Can Dropbox Djinni be used with C++98

I am trying to evaluate Djinni, for generating Java and Obj-C wrappers from our C++ code.
We currently use SWIG and are evaluating other tools for wrapper generation.
Due to some restriction at my work place our C++ code is in C++-98.
We cannot migrate to C++-11 due to some customer needs.
As such I wanted to know if I can use Djinni, with C++-98.
Djinni github readme states:
Interfaces are objects with defined methods to call (in C++, passed by shared_ptr). Djinni produces code allowing an interface implemented in C++ to be transparently used from ObjC or Java, and vice versa.
I saw couple of sample's using Djinni over the internet and they all seem to use shared_ptrs with their interfaces.
Is C++-11 mandatory for using Djinni?
No, Djinni can not be used with C++98. It uses C++11 pretty extensively, both in the generated code and in the support library, so it can't support C++98.

Writing PostgreSQL Functions in C++

I have some problem writing postgres functions in C++ while following the guides for C: C-Language Functions. I found that most postgres functions are written in C instead C++, but I have to use a lib that is written in C++, so I chose C++. My question is, is there anything to notice when writing in C++? It's common to write makefiles using pgxs, so how should I write the makefile to make it work? Thanks.
If you can avoid doing it, do so. PostgreSQL doesn't mix especially well with C++. It's possible, as shown by PostGIS, but it's not overly fun.
If you can, write or generate a pure C wrapper to your C++ library and use that wrapper to interact with the library. That won't be practical if it's heavily template based (eg: boost) or uses other more advanced C++ features, but works well if it's just C-with-objects style code. SWIG can help generate wrappers for you.
If you'd prefer to avoid the wrapper approach or if your library is a bit too complex, too exception-reliant, etc for that then you should read this PostgreSQL manual entry.
Search the PostgreSQL mailing list for more discussion on this topic.

Lua or Python binding with C++

I have used Lua.NET on .NET platform and I could call the .NET class/object from Lua and I could call the Lua from .NET Lua API interface. I did the same with the IronPython. I knew the how the .NET binding works.
Now I have a C++ project and I want to use the dynamic capabilities. I want to call C++ object which may not be possible from Lua so I may need to call some C API which makes call to C++. Meantime I want to call the Lua from C++.
We have configuration data which is best described in table like format in Lua or List & Dict like in Python. We need to enumerate these data structures defined in Lua/Python in C++.
When considering Lua to Python in C++ for two way calling, is Python have upper hand with Boost Python library? I don't have experience in Python/C++ binding. I don't have equal experience of using Python in C++ and calling Python from C++.
When considering Lua to Python in C++ for two way calling, is Python have upper hand with Boost Python library?
There are a few libraries that simplify the communication between C++ and Lua. One of them, luabind, is inspired by boost.python and is quite powerful and fairly easy to use.
Other C++ <-> Lua libraries to consider: toLua++, SWIG
If you are planning to just use windows you could use C++/CLI a managed variant of C++. With C++/CLI you can easily mix managed and unmanaged code. You could call the managed classes from any .net language and the unmanaged (exported) functions from C.

How can I call C++ functions from within ruby

I am an experienced C/C++ developer but I am a novice in Ruby.
How can I call a C++ function from with in Ruby?
You have 3 possibilities :
1) Ruby is able to load libraries. Even if it is a bit tricky, you can decide to write your own loader and bind your C++ library in Ruby.
This is done using what is called an extension module. You will find a comprehensive tutorial here: http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html
2) You can use a tool that will generate the Ruby wrapper around your C++ library. Look at SWIG for example (http://www.swig.org/).
You just have to create a file in a swig-specific syntax and provide it to SWIG. It will then be able to generate the wrapper for many languages, Ruby included.
3) You can choose to use a middleware, such as CORBA/ICE/whatever. It may be a bit overkill if you only want to call some C++ functions, but it will allow you to remote call the functions, or "hide" a grid behind the middleware.
To call C++ code from Ruby, you will likely want to build an extension.
If you are an experienced C++ developer, you may feel comfortable with Rice:
https://github.com/jasonroelofs/rice
It uses C++ metaprogramming techniques to simplify writing extensions.
If you were calling into C, you could also use ffi. Calling C++ code is a little more complicated than calling C code due to name mangling and exceptions.
I believe the questioner is asking how to call C++ from with in Ruby, if so then the for simple C/C++ RubyInline1 is by the far the simplest solution.
Alternatively if you need to call more substatntial C++ code, you can build a ruby extension. Here is a good tutorial
You need to wrap your c++ code in a C interface and then bind those C functions to ruby methods using rb_define_method()
alternatively you can use SWIG, as Aurelien said.