How can I implement interop between OCaml and C++? - c++

I want to create a bridge between OCaml and C++. For instance I want to use some constructions written in OCaml in C++.
How can I achieve this? Are there any libraries, bindings for this?

You should read the relevant part of the language manual: Interfacing C with OCaml. It is quite detailed even if, by nature, painfully low-level.
If you don't need tight communication between C++ and OCaml code (eg. you interface GUI code and computation code, but the computationally intensive kernel of your application does not cross application boundaries, or at least the cost of communication is expected to be neglectible compared to time spent on either side), I would recommend that you explore simpler ways, where C++ and OCaml code run in separate processes, and exchange information through message passing (in whatever format that is most convenient to define: text, s-expressions, binary format, JSON, etc.). I would only try to bridge code in the same process if I'm sure the simpler approach cannot work.
Edit: since I wrote this answer last year, the Ctypes library emerged, from Jeremy Yallop; it is a very promising approach that may be significantly simpler than directly interfacing C with OCaml.

The easiest way to do this is in two steps: OCaml → C and then C → C++ using the extern keyword. I do this extensively in my COH*ML project which binds OCaml with the Coherence library in C++. For example in OCaml I have:
type coh_ptr (* Pointer to a Cohml C++ object *)
external coh_getcache: string -> coh_ptr = "caml_coh_getcache"
Then in C++, first a C function:
extern "C" {
value caml_coh_getcache(value cn) {
CAMLparam1(cn);
char* cache_name = String_val(cn);
Cohml* c;
try {
c = new Cohml(cache_name);
} catch (Exception::View ce) {
raise_caml_exception(ce);
}
value v = caml_alloc_custom(&coh_custom_ops, sizeof(Cohml*), 0, 1);
Cohml_val(v) = c;
CAMLreturn(v);
}
}
And finally the C++ implementation:
Cohml::Cohml(char* cn) {
String::View vsCacheName = cn;
hCache = CacheFactory::getCache(vsCacheName);
}
Going the other way is basically the same principle.

Related

Linux g++ Embedding Prolog Logic Engine Within C++

I have some logic in a C++ program that is not only insanely complex, it requires multiple solutions for which Prolog is ideal. It's sort of like a firewall config script, checking input for actions, but sometimes more that one action is required.
What I want is something like this:
class PrologEngine
{
LoadLogic(const char* filename) throw PrologException; // Load a file of prolog rules, predicates facts etc in textual format. Must be callable multiple times to load AND COMPILE (for speed) prolog rule files.
std::vector<std::string> Evaluate(const char* predicate_in_string_form = "execute(input, Result)") throw PrologException; Returns a vector of matching predicates in text form.
};
It needs no ability to call back into C++.
AMI Prolog seems to get it, but it's not available on Linux. I'm trying to use SWI-Prolog and can only find 2 examples and and incredibly byzantine API (my opinion)
Can anyone point me to an example that is close to what I'm looking for?
There is A C++ interface to SWI-Prolog, that's high level.
I'm fighting with it, here an example of bridging to OpenGL:
PREDICATE(glEvalCoord1d, 1) {
double u = A1;
glEvalCoord1d( u );
return TRUE;
}
This clean code hides many 'bizantinism', using implicit type conversion and some macro. The interface is well tought and bidirectional: to call Prolog from C++ there are PlCall ('run' a query, similar to Evaluate you expose in the answer) or a more structured PlQuery, for multiple results...
If you don't need to link to openGl, or can wait to ear about the answer that hopefully I'll get from SWI-Prolog mailing list, you should evaluate it.
If you don't mind rewriting the prolog code for use in a native c++ header only library, I'd look into the castor library:
http://www.mpprogramming.com/cpp/

gcc for parsing code

I would like to know how to use GCC as a library to parse C/C++/Java/Objective C/Ada code for my program.
I want to bypass prepocessing and prefix all the functions that are user written with a prefix My.
like so Print(); becomes MyPrint(); I also wish to do this with the variables.
You can look here:
http://codesynthesis.com/~boris/blog/2010/05/03/parsing-cxx-with-gcc-plugin-part-1/
This is description of how to use gcc plugin interface to parse C++ code. Other language should be handled in the same manner.
Also you can try pork from mozilla:
https://wiki.mozilla.org/Pork
When I tried it (pork), I spend hour or so to fix compile problems, but then
I can write scripts like this:
rewrite SyncPrimitiveUpgrade {
type PRLock* => Mutex*
call PR_NewLock() => new Mutex()
call PR_Lock(lock) => lock->Lock()
call PR_Unlock(lock) => lock->Unlock()
call PR_DestroyLock(lock) => delete lock
}
so it found all type PRLock and replate it with Mutex, also it search call of functions
like PR_NewLock and replace it with "new Mutex".
You might wish to investigate the sparse C parser. It understands a lot of C (all the C used in the Linux kernel sources, which is a fairly good subset of legal ANSI-C and GNU-C extensions) and provides a few sample compiler backends to provide a lint-like static analysis tool for type checking.
While the code looks very clean and thorough, your task might be easier done via another mechanism -- the example.c included with the sparse source that demonstrates a compiler is 1955 lines long.
For C, you cannot do that reliably. If you skip preprocessing you will -- in general -- not have valid C code to be parsed. E.g.
#define FOO
#define BAR
#define BAZ
FOO void BAR qux BAZ(void) { }
How is the parser supposed to recognize this a function definition of qux without doing the preprocessing?
First, GCC is not a library, and is not structured to be one (in contrast to LLVM).
Why (i.e. what for) do you want to parse C, C++, Ada source code?
I would consider (assuming a GCC 4.6 version) extending GCC either thru plugins written in C, or preferably using MELT, a high level domain specific language to extend GCC (disclaimer: I am the main author of MELT).
But using GCC as a library is not realistic at all.
I really think that for what you want to achieve, MELT is the right tool. However, it is poorly documented. Please use the gcc-melt#googlegroups.com list to ask questions.
And be aware that extending GCC does take some amount of work (more than a week perhaps), because you need to partly understand the GCC internal representations.
Our DMS Software Reengineering Toolkit can parse C, C++, Java and Ada code (not Objective C at this time) in a wide variety of dialects and carry out transformations on the code. DMS's C and C++ front ends include a preprocessor, so you can you can cause preprocessing before you parse.
I'm probably don't understand what you want to do, because it seems strange to rename every function and (global?) variable with a "My...." prefix. But you could do that with some DMS rules (a rough sketch of renames of user functions for GCC3:
domain C~GCC3.
rule rewrite_function_names(t: type_designator, i: IDENTIFIER, p: parameter_list, s: statements):
function_header->functionheader
"\t \i(\p) { \s } " -> "\t \renamed\(\i\) (\p) { \s }" ;
and a helper function "renames" that takes a tree node containing an identifer, and returns a tree node with the renamed identifier.
Because DMS patterns only match against the parse trees, you won't get any false positives.
You'd need some additional patterns to handle various different syntax cases within each langauge (e.g, for C, "void" return type, because "void" isn't a type designator in the syntax, and global variable declarations), and different rules for different languages (Ada's syntax is not the same as that of C).
This might seem like big hammer for your task, but if you really insist on doing this for a variety of languages in a reliable way, it seems hard to avoid the problem of getting decent parsers for all those languages. (And if you are really going to do this for all these languages, DMS can be taught to handle ObjectiveC the same we we have taught it to handle the other langauges).
Your alternative is some kind of string hacking solution, which might work 95% of the time. If you can live with that, then Perl or something similar is likely your answer.
forget about GCC, its made as a compiler's parser, not an analysis parser, you'd do way better using something like libclang, a C interface to clang, which can process both C & C++

CoffeeScript-like language for C/C++

I've been looking for a language that provides the same functionality that Coffeescript has, but for C/C++. I mean a language that converts the code into C, but readable, just like CoffeeScript converts to Javascript, readable and all.
I think this is possible, and even desirable (I grudgingly deal with C++ when writing Node.js native modules), but more challenging than with a higher-level language like JavaScript.
What you're asking for is a language that would provide syntactic sugar without sacrificing performance or flexibility. Some syntactic sugars (say, syntactic whitespace or Ruby-style def/end blocks instead of curly braces) would be trivial to add. But adding anything more advanced, you'd run into two major hurdles: static typing, and garbage collection.
For instance, let's say that you wanted to add implicit returns. It seems like a small feature, but think about it: In order for the feature to be useful, you'd have to—at the very least—throw a compile-time error when the value of the last expression doesn't match the function's return type. That means that your compiler needs to inspect a line like
a->b
and figure out what type it is. That's possible in principle, but it's a heck of a lot more work than the CoffeeScript compiler does.
Or say you added list comprehensions. That means you're allocating an array whose length isn't known at compile-time, which means you'll need to later deallocate it yourself. So the syntactic sugar could actually hurt you. The rule "If you malloc it, you free it" doesn't work if the compiler is adding in the malloc for you, unless it can figure out where to put the free (which, again, is generally possible but would take a lot of work).
So, while I'd love to someone give C++ the CoffeeScript treatment, I don't expect it to happen any time soon—if ever. I think it's more likely that the world will eventually move on to something like D or Go for system-level programming.
I think OOC is probably the closest thing to Coffeescript for C. It's a programming language with a lot of the features you'd expect from dynamic languages (objects, first class functions, clean syntax) that compiles directly into C99.
http://ooc-lang.org/
One item missing from Jacindas list you might want to know about: Vala/Genie is a compiler targetting C with the GObject library implementing objects, written by Gnome. Vala is a C#-like syntax, and Genie a Python-like syntax, but for the rest they are the same system. It was actually created because bare C + GObject became too much of a pain to work with for the Gnome guys. Vala does objects and automatic memory management based on reference counting or ownership tracking, and a lot of other things you'd expect in a C# like language.
As for the CoffeeScript-like property, I just saw that there was an experimental feature to disable the dependency of the generated code on GObject, so it generates just plain C without any runtime dependencies. Doing so disables a number of more advanced OO features, but it still leaves you with a better syntax, a basic object system, and (semi-)automatic memory management.
I don't know how readable the output is, but if you run it through a pretty printer it might be very close to what you're looking for.
SugarCpp is a language which can compile to C++11. It should be what you are looking for. Visit https://github.com/curimit/SugarCpp for more details.
For Python specifically, take a look at this question:
Convert Python program to C/C++ code?
They mention Shed Skin, which will take a subset of pure python and convert to standalone C++ code.
Cython is typically used to create Python extension modules, but can create standalone programs if the Python interpreter is embedded. This doesn't sound like what you're looking for, though.
Cython is based on Pyrex, and they are compatible with each other in many ways.
For some of the other languages you mentioned there seem to be similar projects: Ruby and PHP. Toba for Java (though no longer maintained), Marst for Algol, BCX for BASIC, COB2C, PtoC for Pascal and I should probably stop there before this turns into "List of Converters from Foo to C/C++."
Hope that helps!
Take a look at this fresh new project: https://bixense.com/coffeepp/
Coffee++
Coffee++ is a little language that compiles into C++. It has been created to have something similar to CoffeeScript for C++. Currently Coffee++ is in a alpha state and not at all usable or final. Check out the source on Github to get involved.
The golden rule of Coffee++ is: "It's just C++". The code compiles one-to-one into the equivalent C++, and there is no runtime library. You can use any existing C++ library seamlessly from Coffee++ (and vice-versa).
Overview:
source file Test.cf++
include iostream
int main():
age := 5
dog := Dog(age)
if age != 7:
dog.bark()
class Dog:
public Dog(int age):
this->age := age
public void bark():
std::cout << "Woof!\n"
private int age
};
compiled Test.hpp
#pragma once
int main();
class Dog {
public:
Dog();
void bark();
private:
int age;
};
compiled Test.cpp
#include "test.hpp"
#include <iostream>
int main() {
auto age = 5;
auto dog = Dog(age);
if (age != 7) {
dog.bark();
}
}
Dog::Dog(int age) : age(age) {
}
void Dog::bark() {
std::cout << "Woof!\n";
}
Since vala and genie were already mentioned, I'll put BaCon (Basic Converter) out there for those who reminisce about hand coding programs from a monthly print publication, but want to use it with a modern GUI.
Must run on each Unix/Linux/BSD platform, including MacOSX
Converted sourcecode must be compilable with GCC
Must resemble genuine BASIC with implicit variable delarations
Spoken language constructs are preferred
The website http://www.basic-converter.org/ has lots of examples (some of theme pretty complex for "BASIC") and plugins for nearly every opensource IDE or you can use the BACON IDE.
Well, this is not what you want, but.. : http://www.campbell.nu/oscar/cython/index.html - This cython/cytoc is a significant space (pythonish) transpiler for C/C++ that I coded around 1999/2000, it has no relation to the cython project that arrived seven years later.
Frankly, I wrote it in Perl and it's heuristical, using regular expressions. I used it for an entire project of a Gameboy Color game (regular ansi C). But I wouldn't trust it... Which is why I'm looking around too, instead of using that dusty old bugger ;)
Follow up:
I've been working on Onyx (https://github.com/ozra/onyx-lang) for a year plus now, and finally realized the obvious thing to do is rewrite it to compile to C++ instead of LLVM-IR. The re-target idea is brand fresh, so rewrite is still vapor. But your input would be made well use of in RFC's, if you like the idea of the language, it's your chance to shape it.

wrap a c++ library in c? (don't "extern c")

is it possible to wrap a c++ library into c?
how could i do this?
are there any existing tools?
(need to get access to a existing c++ library but only with C)
You can write object-oriented code in C, so if it's an object-oriented C++ library, it's possible to wrap it in a C interface. However, doing so can be very tedious, especially if you need to support inheritance, virtual functions and such stuff.
If the C++ library employs Generic Programming (templates), it might get really hairy (you'd need to provide all needed instances of a template) and quickly approaches the point where it's just not worth doing it.
Assuming it's OO, here's a basic sketch of how you can do OO in C:
C++ class:
class cpp {
public:
cpp(int i);
void f();
};
C interface:
#ifdef __cplusplus
extern "C" {
#endif
typedef void* c_handle;
c_handle c_create(int i)
{
return new cpp(i);
}
void c_f(c_handle hdl)
{
static_cast<cpp*>(hdl)->f();
}
void c_destroy(c_handle hdl)
{
delete static_cast<cpp*>(hdl);
}
#ifdef __cplusplus
}
#endif
Depending on your requirements, you could amend that. For example, if this is going to be a public C interface to a private C++ API, handing out real pointers as handles might make it vulnerable. In that case you would hand out handles that are, essentially, integers, store the pointers in a handle-to-pointer map, and replace the cast by a lookup.
Having functions returning strings and other dynamically sized resources can also become quite elaborate. You would need the C caller provide the buffer, but it can't know the size before-hand. Some APIs (like parts of the WIn32 API) then allow the caller to call such a function with a buffer of the length 0, in which case they return the length of the buffer required. Doing so, however, can make calling through the API horribly inefficient. (If you only know the length of the required buffer after the algorithm executed, it needs to be executed twice.)
One thing I've done in the past is to hand out handles (similar to the handle in the above code) to internally stored strings and provide an API to ask for the required buffer size, retrieve the string providing the buffer, and destroy the handle (which deletes the internally stored string).
That's a real PITA to use, but such is C.
Write a c++ wrapper that does an extern c, compile that with c++, and call your wrapper.
(don't “extern c”)
extern C only helps you to have a names in dll like you see them.
You can use
dumpbin /EXPORTS your.dll
to see what happens with names with extern C or without it.
http://msdn.microsoft.com/en-us/library/c1h23y6c(v=vs.71).aspx
To answer your question... It depends... But it is highly unlikely that you can use it without wrappings. If this C++ library uses just a simple functions and types you can just use it. If this C++ library uses a complex classes structure - probably you will be unable to use it from C without wrapping. It is because the internal of classes may be structured one way or another depending on many conditions (using inference with virtual tables or abstracting. Or in example complex C++ library may have its own object creation mechanisms so you HAVE to use it in the way it is designed or you will get unpredictable behavior).
So, I think, you have to prepare yourself for doing dome wrappings.
And here is a good article about wrapping C++ classes. It the article the Author tells about wrapping C++ classes to C# but he uses C at first step.
http://www.codeproject.com/KB/cs/marshalCPPclass.aspx
If the C++ library is written which can be compiled with C compiler with slight editting (such as changing bool to int, false to 0 and true to 1 etc), then that can be done.
But not all C++ code can be wrapped in C. Template is one feature in C++ that cannot be wrapped, or its nearly impossible.
Wrap it in C++ cpp that calls that dll, and "extern C" in that file you made.

How do I call a C++ static library from Perl?

I'm writing a C++ static library that needs to be shared among several applications, one of them written in Perl. Unfortunately, I only barely know the core Perl language (I read the Llama book), and I'm not all that familiar with its libraries. How do you make calls to an external C++ binary from a Perl script?
By Google search, I found information on the Perl Inline module, but if I understand it correctly, that isn't what I need. I'm pretty sure that's for writing C and C++ code directly in your Perl scripts, not for calling external C++ libraries.
The C++ static library is being cross-compiled for an ARM processor, so there will be no C++ compiler on the target machine where the Perl script will be running. (If that makes a difference.)
You can call code from other libraries via Inline::C (and likely the same via Inline::CPP) - have a look at Inline::C::Cookbook. Most likely you want to start out with Inline and after you're done experimenting use the resulting .XS file to work further.
You want to look at using XS, which is how Perl normally interfaces with C/C++ libraries. It's not quite trivial. A couple of relevant portions of the Perl documentation:
perlxs
perlxstut
First, it does need to be in a dynamic library, not a static library (unless you'll be re-compiling perl itself and linking it against your static library).
Second, since C++ will mangle the names (one of the most annoying "Features" of C++ if you ask me) you'll need an extern "C" block that contains hook functions. If you were using C++ you could probably get by with a single hook function that returns the C++ object that implements the interface you need to use. Since you're using perl, you may need to wrap an object in an interface like this:
CPPObject object;
extern "C"
{
int InitObject( void )
{
return object.init();
}
int DoCoolStuff( void )
{
return object.DoCoolStuff();
}
int DoOtherCoolStuff( int foo )
{
return object.DoOtherCoolStuff( foo );
}
int DestroyObject( void )
{
return object.Destroy();
}
}
You need to create a wrapper function that is callable from perl, and AFAIK, you'll need to have this wrapper function be in a dynamic library (unless you're going to rebuild the perl binary and link the static lib to it). I like to use a tool called SWIG (Simple Wrapper Interface Generator) to create the wrappers for me. It can create wrappers for 17 or so other languages too.
Probably not what you're thinking, but how about writing a stand-alone C++ program that the perl program communicates through pipes with?
I'm only starting to wrap my head around XS, so I can't offer much help. But here's what I do know...
There is XSpp, which is XS for C++. It is distributed with WxPerl. WxPerl is under active and responsive development.
Inline:CPP can be used to write your initial interface/wrapper code. Then you can analyze the generated XS. However, it doesn't look so well maintianed. If it works, it may provide you with a good head start.
You might find this short note on XS and C++ by John Keiser helpful, if a bit dated.