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.
Related
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.
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.
From the QnA for what a runtime is : What is "runtime"?
I understood about runtime a little.
To make my understanding more robust, I'd like to ask new question.
Is C runtime an essential for any other programs like lua, haskell, java, etc... ?
I know C library implements not only standard C api but also system call wrappers also.
And I know lua runtime is no different than just a C program. This means lua runtime is based on C runtime.
So I guess any other language would be in the same situation. Because it is only C rumtime which can call system calls.
I considered for linux but I guess Windows and other Unix implementations must be same.
Am I right?
Not necessarily.
All programs written in C make use of the C runtime environment, be it the standard library, the stuff happening before the main function, or else.
If a compiler/interpreter/virtual machine is written in C, yes, it does make use of the C runtime.
OTOH, if it isn't, it does not.
Note that you have to go up the whole "compiler/interpreter/virtual machine chain."
A Python interpreter written in Haskell might still indirectly rely on the C runtime environment if the Haskell interpreter has been written in C.
[...] it is only C rumtime [sic] which can call system calls.
No. The C runtime provides wrappers around some system calls. You can write programs in pure assembly making use of system calls and not rely on the C runtime library at all.
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.