7

I like to try out many different programming languages but I can never really use many of them in real projects because all important libraries that I need are written in C++.

Usually I have to write a C-Interface which can be pretty time consuming, so I am wondering does a programming language exist that can seamlessly call any C++ code?

Maik Klein
  • 15,548
  • 27
  • 101
  • 197
  • 2
    Does something like [SWIG](http://www.swig.org/) scratch your itch? – Potatoswatter May 10 '14 at 07:16
  • 8
    Different C++ compilers aren't even compatible among themselves. Any other language is even less so. – Torino May 10 '14 at 07:16
  • @Torino Presumably such a project would compile the C++ code itself, perhaps via a bundled GCC, so cross-compiler incompatibility could be avoided. – cf- May 10 '14 at 07:24
  • We often use Python with SIP, but it's a PITA anyway (the same seems to hold for most other wrapper generators). – Matteo Italia May 10 '14 at 09:02

4 Answers4

5

Many functions in C++ are defined in header files, and expected to be compiled into every application using them. It occasionally happens for C too, but for C++ it is far more common, particularly as class members for templated classes cannot be made available in advance for every potential template instantiation: there are infinitely many.

If compatibility with C++ requires a complete C++ parser and compiler, you've not really got a different language, you've got C++ with extensions. Even if one of those extensions is to make parts of the code look nothing like C++, it adds massive complexity for little benefit.

If you don't need complete and seamless compatibility with C++, then limited compatibility is available in some languages. There is D's Interfacing to C++, describing what works, what doesn't, and why not.

3

There is an experimental language that can do so, but there's only one developer (myself) and it's in such an early stage of development that it's not useful right now.

Here's a simple sample program:

Main() {
    cpp("iostream").std.cout << "Hello, World!";
}

FTR, compiling this sample requires ADL, OR, template instantiation, ABI compliance, parsing the C++ header in question, and lots of fun stuff.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 3
    To make this answer somewhat useful you should at least provide a link to it. – Matteo Italia May 10 '14 at 09:03
  • That presumes that I have built a useful website for it. I haven't. It's a one-man effort in my spare time. I spent my time writing it, not writing about it. I suck at website design anywy. – Puppy May 10 '14 at 09:05
  • 1
    Want any help with it? I need a new project. – RamblingMad May 10 '14 at 09:06
  • The language's not useful means that the answer is useless. The question was not purely theoretical. – cubuspl42 May 10 '14 at 09:19
  • The question doesn't state any such thing. He only asks if it exists. It does. It's just not really ready for prime time just yet. – Puppy May 10 '14 at 09:19
  • @CoffeeandCode: I could sure use some help. Come to the lounge and I'll be glad to discuss it with you. – Puppy May 10 '14 at 10:34
2

No language can seamlessly interop with C++. C interfaces are a good idea. It's important not to export all / many functions from the module, because it's time consuming as you noticed. Focus on solving one problem at time and export the function solving this problem to C.

On the other hand, if you really need to expose many functions or even classes to other language, you can use SWIG. Read its documentation and you'll notice how many problems it needs to deal with while creating the wrapping code. That's because any two languages are different and you can't change that. The solution to this problem is to use the greatest common interface of two languages - C.

cubuspl42
  • 7,833
  • 4
  • 41
  • 65
  • 1
    [C has no standard ABI](http://stackoverflow.com/questions/4489012/), either. – fredoverflow May 10 '14 at 09:01
  • 1
    Guess it's a pity that I've already built a language that can do this. – Puppy May 10 '14 at 09:01
  • Ok, I've edited the part with no standard ABI. But @FredOverflow, note that it has a de facto per platform standard ABI. At least on Linux, Windows and OSX. – cubuspl42 May 10 '14 at 09:08
  • @DeadMG You can build a language that has a C++ compatibility in mind, but will it link with Mingw objects? Will it link with MSVC objects? Will it be compatible with both libstdc++ and libc++ on OSX? Do you support SEH and DWARF2 exceptions? Do you support templates? If so, you build the language that seamlessly interops with c++. – cubuspl42 May 10 '14 at 09:15
  • @cubuspl42: Some of those are a work in progress, but some are already done. I do support templates, I do link with MinGW objects, and I am compatible with both libstdc++ and libc++ (although probably not OSX, I only built on Linux and Windows so far). As for MSVC's ABI, I can't even begin to work on it until Clang's support for it is complete. – Puppy May 10 '14 at 09:19
-1

The only solution that meets your requirements is V8 / Javascript from Google.

Is the only implementation of a scripting language written in C++ that I know of, plus the API themselves are exposed via C++ too .

The downside is that is not a simple project, is not exactly available on any platform, it's being developed according to the Google needs and there are no guidelines or standards, for example C++ is being versioned by ISO documents that are released each time a new milestone is out and each one of them defines the standard, V8 is more like an ongoing project with some big voids, especially in terms of invariances , standardization and concurrency.

For example there is nothing that keeps Google from changing the entire set of APIs from one version to the other, and unless you are counting on a really big team, I doubt that you can keep up with that.

user2485710
  • 9,451
  • 13
  • 58
  • 102
  • Man, did you try to integrate C++ with V8? It's same sh*t as with Lua, CPython, everything. +No stable API. It's just... implemented in C++. – cubuspl42 May 10 '14 at 09:22
  • 1
    @cubuspl42 well the OP asked about seamlessly interoperability with C++ not for a simple solution :D . If you know any other language with a C++ implementation you are welcome to report it, I don't know of anything else written in C++. – user2485710 May 10 '14 at 09:23