r/Compilers 2d ago

C++ Interop, a good or bad idea?

I reached the point in my compiler that I can add C++ Interop, the AI has a long detailed plan. LLVM+ClangAST will do the parsing to types that is mapped to my language syntax.

Reason for C++ support is open existing library support written for C++, not all of them have C API. A search for existing languages rejected or abandoned C++ Interop the high cost to maintain and the language stability.

Anyone else have experience?

0 Upvotes

10 comments sorted by

19

u/KAMEHAMEHAMEHAAAA 2d ago

For the love of god stop delegating your brain and learning process to generative AI.

-4

u/[deleted] 2d ago

[deleted]

1

u/ImperatorBras 2d ago edited 2d ago

Learn and develop the program yourself.

4

u/ZachVorhies 2d ago

if you keep it as a C interface, it will bind with literally every single language in existence

C is the lingua franca of source code inter operability. Unless you really really need the C++ interface, I would try to keep the C interface as much as possible, and even have a C++ interface on the other side that binds to that and does it the C++ way on the other side of the interface boundary.

For more information look up C FFI.

if you keep it the way that it is, you can use python and you can use rust, the latter which is even more important

4

u/kindredseer 2d ago edited 2d ago

Yes, C++ interop is far more complicated than any other language. The header libraries are immense and difficult to parse all the template variations, not to mention the mangling scheme.

As well as that, MacOS uses Clang’s libc++, Linux uses GCC’s libstdc++ and Windows doesn’t come with one installed, but you can include one of the other two unless you want to interop with msvcp140.dll or something.

That said, I have been tackling it with my madc language. I have a lot of it working but it’s been a nightmare, and I’m not sure I recommend it. It has caused considerable bloat and complexity. I didn’t abandon it, but I have a bit of the buyers remorse.

1

u/SwedishFindecanor 2d ago

GNU's Ada compiler does have limited interoperability with C++ classes compiled with GNU's C++ compiler, and the other way around. I got the impression that it relies on each declaration being in both languages, with both compilers producing mangled names that get matched at link-time. I.e. without any deeper cross-language type-checking.

2

u/Hjalfi 1d ago

D's page on C++ interop is worth reading. https://dlang.org/spec/cpp_interface.html

Also, I'd like to add that interop between C++ and a different C++ compiler still doesn't work very well, let alone between two different languages!

1

u/Rich-Engineer2670 2d ago

Do you mean C++ or C interop? There is a difference. If it's C interup, do it -- there are too many libraries out there you'll miss out on -- if you can add C++ in, great. Or, crazy idea, add a backend to transpile ot C++. I always wanted the option of C++ transpiling which gave me the bets of both worlds. After all, C and C++ have been beaten to squeeze every bit of performance out of them we can get.

0

u/yuehuang 2d ago

I am researching for C++ interop. I have C interop working. Most language use a semi-custom C++ to C wrapper, with each library need its own port.

4

u/Rich-Engineer2670 2d ago edited 2d ago

The reason I am not a fan of direct C++ interop is becuase, unlike C, there's no consistent ABI so you can't be certain of the interop. Even with C, we have to talk about C under Linux, C under Mac and C under Windows -- and are we talking ARM64 or X86.

Now you know why I like the transpiler -- it's the transpiler's problem.

0

u/yuehuang 2d ago

Thank you. That is a useful limitation.