Calypso and the future of D
Kelly via Digitalmars-d
digitalmars-d at puremagic.com
Sun Jan 25 03:12:48 PST 2015
On Sunday, 25 January 2015 at 10:18:34 UTC, Walter Bright wrote:
> I'm sorry, but it still far from clear just what Calypso does.
>
> Suppose I have a C++ file, test.cpp, that contains:
>
> int foo(unsigned *p);
>
> How do I use Calypso to interface with that from D:
>
> ... what happens here ? ...
> uint x;
> foo(&x);
>
>
> Next, in the README example, it says:
>
> $ clang++ -std=c++11 -c showcase.cpp -o showcase.cpp.o
> $ ar rcs libshowcase.a showcase.cpp.o
> $ ldc2 -cpp-args -std=c++11 -Llibshowcase.a -L-lstdc++
> showcase.d
>
> Where's Calypso in that?
Calypso is part ldc2 and, as far as I understand it, is invoked
via the "-cpp-args" command line arg.
I believe the simplest example that shows the use of your
function above is as follows:
-----------------------------------------
test.h
-----------------------------------------
namespace test {
int foo(unsigned int *p);
}
-----------------------------------------
test.cpp
-----------------------------------------
#include "test.h"
int test::foo(unsigned int *p)
{
return *p * 2;
}
-----------------------------------------
test.d
-----------------------------------------
modmap (C++) "test.h";
import (C++) test._; //this imports global vars, funcs and
typedefs
import std.stdio;
void main()
{
uint x = 4;
writeln("foo = ", foo(&x));
}
-----------------------------------------
BUILD
-----------------------------------------
#!/bin/bash
clang++ -std=c++11 -c test.cpp -o test.cpp.o
ar rcs libtest.a test.cpp.o
/bin/rm calypso_cache*
ldc2 -v -cpp-args -std=c++11 -Llibtest.a -L-lstdc++ test.d
-----------------------------------------
-----------------------------------------
The above was cut-n-pasted from a test dir. Tested on Linux.
Produces a 'test' executable that prints 'foo = 8' when run.
I have tested more complex examples and things seem to work quite
well. The one big caveat that I have run into so far with Calypso
is that everything must be in a namespace...that is why test.h
has the somewhat superfluous 'test' namespace. It must be there
to use Calypso, as far as I can see.
This can be an issue when trying to import 'C++' libraries if
they just use .h files like glorified C header files with some
classes in them, or something like that, and they don't
encapsulate everything in a unique namespace.
I am sure Elie will chime in, but I am definitely impressed and
inspired by what he has accomplished here. Hopefully I haven't
butchered the above explanation too much since I am not a Calypso
expert :)
Thanks,
Kelly
More information about the Digitalmars-d
mailing list