Using Delphi library with C++ Builder

Since early XE versions C++ Builder supports Delphi units directly added in C++ project and compiles them as well as C/C++ source. However, this approach has several drawbacks:

  • Usually, Delphi files are stored in separated folders of other applications and packages. So your C++ project will point to many relative or absolute paths beyond the project root folder
  • Delphi units may have many dependencies. Therefore, you should include them all into your C++ project as well

The solution is to group Delphi units into package library and then use it in C++ Builder project with a minimum of dependencies. The example below is tested with Delphi/C++ Builder Berlin 10.1 but should work with previous versions too.

Prepare project directories

I suggest the following folder structure for my test example.


MyCBuilderApp and MyDelphiLib are the subdirectories for corresponding projects. Folders Include and Libs will store shared generated headers and libraries.

Create Delphi package library

  • Start Delphi and create new Delphi project of "Package" type
  • Save project as MyDelphiLib.dproj into MyDelphiLib folder
  • Add new Delphi unit and save it as MyDelphiUnit.pas in the project folder
  • Change project settings and specify C++ generation options
  • Write unit code as in following example
  • Build project and verify that HPP and LIB files are generated

MyDelphiUnit contains the class THello that prints out the messages using an assigned event handler or a delegate class implementing the output. The implementation of the output method is injected into the class constructor so you can see a dependency injection pattern at the same time.

unit MyDelphiUnit;
  TOnMessageEvent = procedure (const Msg: string) of object;
  TMessageDelegate = class(TObject)
    procedure DoMessage(const AMsg: string); virtual; abstract;
  THello = class(TObject)
    FOnMessage: TOnMessageEvent;
    FDelegate: TMessageDelegate;
    procedure DoMessage(const AMsg: string);
    constructor Create(const AOnMessage: TOnMessageEvent); overload;
    constructor Create(const ADelegate: TMessageDelegate); overload;
    destructor Destroy; override;
    procedure PrintHello;
{ THello }
constructor THello.Create(const AOnMessage: TOnMessageEvent);
  inherited Create;
  FOnMessage := AOnMessage;
constructor THello.Create(const ADelegate: TMessageDelegate);
  inherited Create;
  FDelegate := ADelegate;
destructor THello.Destroy;
  inherited Destroy;
procedure THello.DoMessage(const AMsg: string);
  if Assigned(FOnMessage) then
  else if Assigned(FDelegate) then
procedure THello.PrintHello;
  DoMessage('Hello from MyDelphiLib!');

Create C++Builder application

  • Start C++ Builder and create new console application. When prompted by project wizard, specify VCL framework to use.
  • Save project as MyCBuilderApp.cbproj into MyCBuilderApp folder as well as main.cpp and header files
  • Change project options for "include" and "library" search paths
  • In the main file include MyDelphiUnit.hpp as well as corresponding library
  • Write some C++ code that uses Delphi class as in following example
  • Build project and run it

#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#pragma argsused
#include <tchar.h>
#include <stdio.h>
#include <MyDelphiUnit.hpp> // Delphi-generated header
#pragma comment(lib, "MyDelphiLib.lib") // Delphi-generated library to link
using namespace std;
class MyDelegate : public TMessageDelegate
    virtual void __fastcall DoMessage(const UnicodeString Msg)
      wcout << L"DC: " << Msg << endl;
class HelloCall
    void __fastcall OnMessage(const UnicodeString Msg)
      wcout << L"FP: " << Msg << endl;
    void Hello()
      THello *hello = new THello(&OnMessage);
      delete hello;
int _tmain(int argc, _TCHAR* argv[])
  // Function pointer (event) implementation
  HelloCall hc;
  // Delegate class implementation
  MyDelegate *delegate = new MyDelegate();
  THello *hello = new THello(delegate);
  delete hello;
  delete delegate;
  return 0;

If all sources were built with no errors, you should see the following output on Windows console.

Debugging Delphi package sources in C++Builder

You may need to step into Delphi package sources from C++ code sometimes. Open Delphi package project settings, select target debug configuration for all or specific platform and check following options.

  • Delphi compiler
    • Compiling
      • Optimization = "false"
      • Debug information = "Debug information"
      • Stack frames = "true"
      • Use debug DCU's = "true"
    • Linking
      • Debug information = "true"
      • Include remote debug symbols = "true"

Save settings and rebuild Delphi package.

In C++Builder project options:

  • go to Debugger/Source path;
  • add paths to Delphi sources.

Restart C++ Builder and recompile the project. From now you should be able to step into Delphi source code.


Using Delphi code in C++ Builder is pretty easy but you should structure your packages to share them with a minimum of dependencies. Adding Delphi files directly into C++ Builder project is an excusable solution only for standalone Delphi units with a small number of dependencies in the uses section that won't be shared for other projects later.