PDA

View Full Version : Combining C++ and Delphi



Thyandyr
01-06-2017, 12:04 PM
Does anyone have experience on projects utilizing/mixing both Delphi and C++?

There is little about it online and all of it comes from Embarcadero ( https://youtu.be/6f5UBL0bQ9U?t=420 ) , so a second opinion would be useful. Does it work as they say, or are there problems they are not mentioning?

I ask this as a long term consideration, I update my Delphi every few years, and I could upgrade to full RAD studio next time, in case it is useful.

Ñuño Martínez
02-06-2017, 07:14 AM
AFAIK it only works if your C++ compiler is Embarcadero's one too (i.e. RAD Studio).

Super Vegeta
03-06-2017, 09:41 AM
I've done this before with C (using gcc) and FPC. C++ would be a bit more tricky since there's name mangling involved; you'd have to wrap all the functions you want to call from Pascal code in 'extern "C" {}' blocks.

Basically I compiled the C code to .o files, and in my Pascal code I put all the functions I needed calling as cdecl (since the calling conventions have to match) - but you can also do it the other way, use "alias" (or was it "name"?) in Pascal code to force a link-name for a function, and then define it as extern in the C code. Either way, later on one can either use {$LINK} in the Pascal code, or the -k command-line option to let the linker know it must include the .o file in the executable. Note that this process requires FPC to build the executable. Should you want to compile some Pascal code, and then build the final executable with gcc, things may not be so simple.

LP
03-06-2017, 02:48 PM
In a company that I've been working with, this approach has been used for over 8 months already: most work is done on C++ side and then exported via simple DLL interface (flattened C-style methods) to Delphi. Generally C++ compilers produce much more efficient code (see (1) (http://forum.lazarus.freepascal.org/index.php/topic,32642.msg241474.html#msg241474) and (2) (http://forum.lazarus.freepascal.org/index.php/topic,36342.msg242126.html#msg242126)), so the performance is better than never and you have overall piece of mind that code is sufficiently future-proof. It seems that C++ community has grown massively since introduction of C++11 (and C++14, now C++17) and with large investments from big players like Google, Apple and Microsoft, it is not going anywhere any time soon.

Ñuño Martínez
03-06-2017, 05:49 PM
What Super Vegeta and LP said works, but only with C code, this is no classes nor objects. When people ask about C++ I always think in classes, and that's not very portable.