5

Is there a way to compile C code from the Delphi IDE?

I would like to use some C code as part of a Delphi project (and would prefer not to put it in a DLL). So, in the Delphi IDE, can I compile C code? Perhaps I can use a keyword to indicate the beginning of the code insert (like ASM does)? Or put the C code in a separate unit - but how would I invoke it from my Delphi code?

If it can be done, how do I go about it? Thanks ...


[Update] Although it sounds like it can be done with the Free Bolrand Command Line C++ Compiler I decided in the end to put the C code into a DLL and statically link that. I guess that either solution is acceptable )?)

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • 3
    if the version of your Delphi IDE has "Build Events", you should be able to use the "Pre-Build Event" to invoke your favorite C compiler and generate your object files that you can staticly link in your delphi code.(see @KenWhite's answer) –  Dec 13 '12 at 04:50
  • 2
    Yep, pre-build event invoking a C compiler. The free Borland C++ 5.5 is a decent option for 32 bit. But that's 32 bit only. If you want 64 bit too, there is MSVC from the Windows SDK. Mingw produces .obj files that the 64 bit Delphi won't link. – David Heffernan Dec 13 '12 at 07:17
  • 2
    Your question is an excellent example that you need to specify the Delphi version, in the text or in the tags or both! – Jan Doggen Dec 13 '12 at 08:49
  • 2
    @Jan Why? The answer is the same for all versions of Delphi that I know. – David Heffernan Dec 13 '12 at 16:40
  • 1
    Weren't pre-built events absent in earlier versions? Also, IIRC there was a more recent switch to MSBuild... – Jan Doggen Dec 14 '12 at 08:45

1 Answers1

12

No, it is not possible to compile c code as part of a Delphi project.

You can compile c code using C++ Builder, and use the object (*.obj) files in Delphi apps by linking them in with {$L file.obj} and then call them just like you would any other Delphi function, but you have to provide implementations of the c runtime library functions yourself. (There are some available in the crtl.pas unit (System.Win.crtl.pas in XE2/XE3); any that aren't there you need to write Delphi replacements for yourself).

For examples of how to do this, you can take a look at how ZLib is used by looking at Zlib.pas (System.ZLib.pas in XE2/3).

As others mentioned in the comments to your question, you can set up a pre-build event to run your c compiler just before your project is built, which will work; technically, I'm not sure if that counts as "compiling as part of your project" or not, since it's actually running an external process. The information above still applies in that case, though.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Do you know if I could link an object file created by the free Borland C++ Compiler? Or, indeed, any other Windows C compiler? – Mawg says reinstate Monica Dec 17 '12 at 02:10
  • 1
    David Heffernan already mentioned that the free compiler will work (in a comment to your question above). I don't know what more information I can provide. MS compilers won't work; they produce a different format `.obj` file that isn't compatible with the Borland compiler's format. (There's a utility for conversion from MS's `COFF` format to the Borland `OMF` format in the Delphi Bin folder for some versions of Delphi.) – Ken White Dec 17 '12 at 03:25
  • 1
    Forgot to mention that the conversion utility is `COFF2OMF.exe`. – Ken White Dec 17 '12 at 03:29
  • 2
    Starting with XE2, you [can link with COFF](http://stackoverflow.com/a/7274582/11480) object files. – Ondrej Kelle Dec 21 '12 at 03:57
  • @TOndrej: Thanks. I didn't know that. :-) I'll tuck that away for future use. – Ken White Dec 21 '12 at 12:04