Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to compile C code from the Delphi IDE as part of a Delphi project?

Tags:

delphi

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 )?)

like image 507
Mawg says reinstate Monica Avatar asked Dec 20 '22 12:12

Mawg says reinstate Monica


1 Answers

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.

like image 194
Ken White Avatar answered Dec 24 '22 01:12

Ken White