I have been using SWIG to make a C library accessible from other languages.
The problem I have encountered, is that I need the programmer in the ported languages to be able to override a function which will then be called by some of the functions in the native c code.
So user overrides method in X Language, and the native c code calls this function. What is the best way to go about doing this?
In particular I am looking to be able to do this for scheme.
Related
I've looked for C APIs, but I've only found C++ API's that can't be used inside a C code.
Are there any C (not C++ or C#) APIs or another way for using AMPL in a C code?
There being no C API for AMPL as far as I can tell, your best bet is to write C++ wrappers for the tasks you want to perform, assigning them C linkage (extern "C") and building them with a C++ compiler. Done correctly, these will be callable from C code. The C code will not be able to handle AMPL objects directly, however, so your wrappers will need to perform some kind of data marshaling in both directions.
I do recommend wrappers specific to your particular tasks, as opposed to generic wrappers for the whole API. I suspect that you would find the latter a much larger and more difficult task.
Is using the LuaJIT library better than using the lua C official library in terms of performance ?
From I have read (the only what was possible), the luajit's feature is only the FFI interface. But I have also read that luajit adds some optimizations to the standard lua-c API but could not find any information about this unfortunately.
I don't understand, does it impact the performance when used without its FFI interface? There is also no any information about how it compiles the code, during the lua_loadfile or anywhen else?
To be more concrete, I want to use lua via the C api and not the luajit FFI interface by making a shared library which lua script can use then.
My general question is this: what's the most common way to include libraries in other languages in a C application?
For example, if I have a Ruby library intended for doing function X, and a Python library for doing function Y, how can I write a program in C (the language, that is) that uses the functions in each?
I've seen wrappers that give access to C libraries in these higher languages, but are there wrappers that go the other way? Is there a common way of handling this in general?
Are these native-code libraries (i.e. have they been compiled?) Or are these source libraries (i.e. a bunch of text files containing Ruby source code)?
If the former, libraries in a language like Ruby or Lua or so usually have a published binary interface ("ABI"). This is low-level documentation that describes how their libraries and its functions work under the hood. Often, those are defined in C or C++, or whatever language was used to implement the interpreter/compiler for Ruby itself.
So you'd have to find that documentation, and find out how to call the parts you are interested in. Some languages even use the same ABI as C does, and you just need to create a header file that matches the contents of the library and you can call it directly (This is how you integrate e.g. assembler and C, or even C++, which you can get to generate straight C functions).
If the latter, you usually need to find an embeddable version of the language, and find out how to run a script from inside your application (This is how Lua is usually used, for example).
But are you sure you need the given Ruby libraries? Often, common libraries are implemented using a C or C++ library under the hood, and then just wrapped for scripting languages, so you can just skip the scripting translation layer and use the (maybe slightly more low-level) library yourself.
PS - there are also automatic wrapper generators, like SWIG, that will read a file in one language and write the translation code for you.
The GObject Reference Manual states that the GObject system was designed to export functions written in C to other languages by using some generic glue code. It is also noted that this glue exists for perl and python explicitly. Omitted however, is how exactly where to find and how to use it.
So, lets suppose I have written a new GObject (for the sake of simplicity, the example given in the same manual) complete with C sources and header files, compiled it, and appropriately installed it, locating it where system libraries are to be found. Now I want to instantiate and use the object in a Python program. Or a Perl Program. Or even a Java program. Or any other programming language that has glib bindings available. How exactly can this be done?
Note that I want to use the object directly, most probably through the already existing generic glue code. I am aware of the possibility to use DBus to export the object from a running C program and access it with Python. But I look for no IPC-Solution. Compiled C library objects shall be more or less directly exported to another programming language.
You're looking for GObject Introspection. Once you have that set up properly you can use PyGObject (Python), Gjs (JavaScript), Vala, etc. pretty easily.
All the languages are, obviously, different, but since you sound most intested in Python... the Python GTK+ 3 Tutorial explains the process using GTK+ as an example.
Here's a few examples below. GType and GObject are standards and do not provide the glue code. You'll just want to look for the language you want to use and see if anyone has implemented the glue yet. If not, maybe you can :)
https://wiki.gnome.org/PyGTK/WhatsNew28
http://search.cpan.org/~rmcfarla/Glib-1.020/GType.xs
I have a project wherein i need to write an application for an USRP device. But the gnuradio software which i use to interact with the device driver and ultimately the hardware provides apis in c++ and python. I am comfortable programming in c and thus would like a way which would let me to call the apis from my c program.Is there a way in which i could do so? It would be a lifesaver.
C++ adds a lot to C, but doesn't take much away. Except for a few corner cases (which can be worked around) most valid C code is also valid C++ code, so you won't encounter many problems when you compile it as C++. Just write your whole project in a C style and only use C++ features where they are necessary to interface with the C++ API of gnuradio.