Do you know you can use Delphi or Pascal files in your C++ applications?
RAD Studio is a very powerful IDE that comes with Delphi and C++ Builder. Delphi uses a version of the Object Pascal programming language while C++ Builder uses mainly C++. If you have Rad Studio you can easily use Delphi functions in your C++ Projects.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files and LSP support for code. C++ Builder comes with Rapid Application Development Studio, also known as RAD Studio, and C++ Builder is one of the most professional IDE’s that work under RAD Studio. C++ Builder followed in the footsteps on the hugely popular Borland TurboC. Under the Embarcadero brand it expanded on the early days of TurboC to add tons of new features. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs. There is a free C++ Builder Community Edition for students, beginners and startups.
You can download the free C++ Builder Community Edition here: https://www.embarcadero.com/products/cbuilder/starter.
Professional developers can use the Professional, Architect or Enterprise versions of C++ Builder. Please visit https://www.embarcadero.com/products/cbuilder.
Table of Contents
How to compile Delphi projects in C++ Builder?
First, let’s create a new Pascal file.
- Create a new pascal file (i.e. pascal_example.pas)
- Add unit name as same as this file name (here we should use ‘unit pascal_example’). Note that this should be same as file name.
- Let’s assume that this pascal file has mypascal_function() and we want to use it in our C++ Builder project. Pascal file should be like this,
pascal_example.pas
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
unit pascal_example; interface function mypascal_function(): Boolean; implementation function mypascal_function(): Boolean; begin WriteLn('This is from Pascal'); Exit(True); end; end. |
Now create a new C++ Console app
- Create a new C++ Builder Console Project (cpp_example.cpp) and save all files into a folder
- Let’s simplify our C++ file as below
1 2 3 4 5 6 7 |
#include <iostream> int main() { getchar(); return 0; } |
3. Press add file to project (or Shift+F11) to add pascal unit that we generate as above.
4. Now we should modify our C++ file. We should imports pascal unit so we can call it our our main() procedure as below,
1 2 |
#include "pascal_example.hpp" #pragma link "pascal_example" |
Thus our C++ file should be as below
cpp_example.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> // Let's import hpp file from pascal and let's link the pascal output #include "pascal_example.hpp" #pragma link "pascal_example" int main() { // let's call the mypascal_function() from the pascal unit mypascal_function(); getchar(); return 0; } |
5. C++ Builder auto generates ‘pasacal.example.hpp’ header file for this pascal unit as below,
pascal_example.hpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// CodeGear C++Builder // Copyright (c) 1995, 2021 by Embarcadero Technologies, Inc. // All rights reserved // (DO NOT EDIT: machine generated header) 'pascal_example.pas' rev: 34.00 (Windows) #ifndef Pascal_exampleHPP #define Pascal_exampleHPP #pragma delphiheader begin #pragma option push #pragma option -w- // All warnings off #pragma option -Vx // Zero-length empty class member #pragma pack(push,8) #include <System.hpp> #include <SysInit.hpp> //-- user supplied ----------------------------------------------------------- namespace Pascal_example { //-- forward type declarations ----------------------------------------------- //-- type declarations ------------------------------------------------------- //-- var, const, procedure --------------------------------------------------- extern DELPHI_PACKAGE bool __fastcall mypascal_function(); } /* namespace Pascal_example */ #if !defined(DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE) && !defined(NO_USING_NAMESPACE_PASCAL_EXAMPLE) using namespace Pascal_example; #endif #pragma pack(pop) #pragma option pop #pragma delphiheader end. //-- end unit ---------------------------------------------------------------- #endif // Pascal_exampleHPP |
6. Now we can compile and run our project.
Is there an alternative method to compile Delphi programs in C++?
Another method to combine these files is running RAD Studio Command Prompt and compiling and linking files by using compiler commands and their options. In this method you must professionally know options and using compiler commands well. After doing steps in A and B Sections;
- Run RAD Studio Command Prompt
- Goto your project folder
- Type this command to console to compile pascal file as below,
1 |
dcc32.exe -jphne -$O+ -$D0 -$L- -$Y- pascal_example.pas |
4. Type these commands below to compile cpp file,
1 2 3 |
bcc32c.exe -tJ -O2 -S cpp_example.cpp bcc32c.exe -tJ -O2 -c cpp_example.cpp bcc32c.exe -tJ -O2 -o cpp_example.exe cpp_example.cpp |
5. Now you can run your executable by typing cpp_example.exe , here is the output.
Here is a third way to compile Delphi programs using C++ Builder
We can also use the RAD Studio Command prompt.
You can write these commands described in section C to a .bat file (which is called DOS batch file). Thus you can run this bat file to compile and link all with a one build command. To do this, After doing section A and B steps, create a new text file which name has build.bat ( !be sure that it is not build.bat.txt) and write all commands that we do inside as below
build.bat
1 2 3 4 5 6 7 8 9 10 |
@echo off setlocal REM Batch file dcc32.exe -jphne -$O+ -$D0 -$L- -$Y- pascal_example.pas bcc32c.exe -tJ -O2 -S cpp_example.cpp bcc32c.exe -tJ -O2 -c cpp_example.cpp bcc32c.exe -tJ -O2 -o cpp_example.exe cpp_example.cpp |
To compile both cpp and delphi (pas) files;
- Run RAD Studio Command Prompt
- Go to your project folder which has codes above
- run ‘build.bat‘ to compile both .pas and .cpp files into cpp_example.exe
- run ‘cpp_example.exe’ ,
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition