3

While developing a new product, we decided to go for a mix of C++ and C#, haven been told that bridging them to allow the C# code to call the C++ code would be easy (spoiler, it's not).

We're pretty experienced C++ programmers and not at all C# programmers so we pretty much just had to believe what we've read. A few attempts to call C and Objective-C was promising and we even found a few articles that showed how to make an unmanaged C++ class available in C# -- or at least we thought. The C++ code in the articles, wasn't C++, but instead the horrible monster C++/CLI that Microsoft seems to think is C++. Since we're doing the C# stuff to get some bits "for free" in macOS and Windows, C++/CLI isn't an option either :-(.

Anyway, plenty of people have claimed that it's easy to call C++ code from some specific programming language, but so far, I haven't seen a single one that will allow me to do so (I haven't been paying too much attention to this, so please provide me with an obvious example). C++ invariably always means C with no C++ stuff at all; no namespaces, classes, no stl, lambdas or anything. Just plain dumb C.

So, are there any languages, besides C++(/CLI) that will allow me to do the following:

  1. Create an instance of a class, using a C++ constructor, and dispatch it with a C++ destructor.

  2. Call member functions on an object ( foo f; f.foo();) with a C++ class.

  3. Use std::vector, std::find_if, std::string and other stuff from the stl. Complete coverage of the stl is not required.

  4. Use overloaded functions (i.e. f(), f(int), f(std::string))

  5. Use overloaded operators (foo operator + (foo, foo))

  6. Use C++11, C++14 and/or C++17 features.

  7. Use RAII (rather important IMHO).

  8. Use namespaces.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Clearer
  • 2,166
  • 23
  • 38
  • Hm, define "easy". You can pass anything from and to Python for example, stuff like swig or boost.python helps with that. If that counts as "easy" I don't know. – Baum mit Augen Jan 19 '18 at 13:42
  • @BaummitAugen I gather OP is after the opposite (calling C++ from Python, for example). – Quentin Jan 19 '18 at 13:42
  • @Quentin Works either way, with swig at least. – Baum mit Augen Jan 19 '18 at 13:43
  • 5
    Different implementations of C++ **can't** call code generated by each other, in general – Caleth Jan 19 '18 at 13:47
  • i guess you have one of 2 options here: 1) rewrite your program in the target language; 2) call c-bound functions from the target language and move around simple values and pointers returned from them. Though some languages, like Java would require an additional layer between the languages. – Serge Jan 19 '18 at 13:59
  • If you want to develop plugins to PHP, Java, Oracle, Python, Whatever then settle for C and it will work. With C++, it won't work. – Lorinczy Zsigmond Jan 19 '18 at 14:31

5 Answers5

8

No. There is no such language.

Unless you count Objective-C++. But that falls pretty much in the same bucket as C++/CLI, in being C++ with some extensions. And C++/CX is another such beast.

There are some interop tools that can work with C++ classes (SWIG for example), but I have never heard of a tool that is capable of instantiating C++ templates (like vector or find_if) on demand.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
5

What languages will call C++ with no explicit bridging?

The short answer to this question is: NONE

Remember that a programming language is a specification written in some technical report, usually in English. For examples, read n1570 (the spec of C11) or R5RS (the spec of Scheme). For C++, see n3337.

Actually, you are interested in implementations, e.g. in compilers and interpreters for your programming languages. These implementations are practically software. And then the answer might become: it depends (notably on the ABI used & targetted by your compiler and system).

See for examples this list of ABIs for Linux.

plenty of people have claimed that it's easy to call C++ code from some specific programming language,

The C calling conventions are quite common, and it might help to declare every C++ function callable from outside as extern "C". But there is no silver bullet, and details matter a lot.

So, are there any languages, besides C++(/CLI) that will allow me to do the following:

list of C++ features skipped

Probably not.

You probably need at least to understand more about memory management approaches. I recommend understanding more about garbage collection, e.g. by reading the GC handbook (at least for underlying concepts & terminology). Learn more about foreign function interfaces (in some cases, the libffi might help) and language bindings.

You might also consider generating some of the C++ or C glue code, maybe with SWIG (or write your own C++ glue code generator).

On operating systems providing dynamic linking capable of loading plugins at runtime (e.g. Linux with dlopen(3)/dlsym(3); but other OSes often have similar facilities) you could even consider generating some C or C++ glue code at runtime in some temporary file, compile it as a temporary plugin, and dynamically loading that plugin. You could also consider JIT-compiling libraries like GCCJIT or LLVM (or libjit).

I recommend reading SICP, the Dragon Book, and probably Lisp In Small Pieces. Of course, learn something about OSes, e.g. Operating Systems: Three Easy Pieces. Reading about Linkers and Loaders could also help.

As an excellent example of cleverly gluing C++, look into CLASP and see this video.

But whatever approach you take, you'll need a lot of work (years, not weeks).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

C++ as a language does not have a defined ABI (Application Binary Interface) - which basically means that there is no universal standard of what a C++ class/function call/template would look like in binary form on any given platform, or across platforms.

What that means is that there is no universal way to call C++ code from other languages, on different platforms, or even across compilers on the same platform. It also means that the people who are telling you "it's easy to call C++ code from XYZ language" are mostly incorrect (or at least incredibly incomplete).

Where there are interfaces it's either because the provider of the interface controls the ABI (C++/CLI with .NET), or because there is a translation layer from C++ to something like the C calling convention (Boost::python).

Some work has been done towards attempting to define an ABI per-platform (http://open-std.org/JTC1/SC22/WG21/docs/papers/2014/n4028.pdf), but as far as I'm aware it has not yet been accepted into C++17.

Joris Timmermans
  • 10,814
  • 2
  • 49
  • 75
1

You can look into using C++ interpreter, which allows for the fine-grained control you ask for. But I don't know of any that "just works", see also: Have you used any of the C++ interpreters (not compilers)?

Juraj
  • 860
  • 4
  • 16
0

D programming language is very close: https://dlang.org/spec/cpp_interface.html

Felix F Xu
  • 125
  • 7