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 )?)
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 thecrtl.pasunit (System.Win.crtl.pasin 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.pasin 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.