delphidlldcu

Compiling multiple units in a single DCU


Coming from a .NET background, now working with Delphi. In .NET, I used to have a separate library project with many classes seperated into distinct files. When I compiled this project, I got a single DLL file.

However, I am not able to do that in Delphi. I have many classes, each in their own file with their own unit name (I did not find a way to have multiple files with the same unit name). When I compile my project, I have one DCU file (I believe this is the equivalent of a .NET DLL?) for each unit in my project.

The problem is that I don't want to have that many files! I would like to have only one DCU file for all of my classes that are in distinct units. Is there a way to achieve what I want to do? How do you deal with these DCUs?


Solution

  • .DCU (Delphi Compiled Unit) files are binary files that are used between compiling source (text) and linking the executable. They're created by the compiler, joined together in memory, combined with the startup code and put into an actual executable (.EXE/.DLL/.BPL) by the linker.

    .DCU files are not .DLL (Dynamic Link Library) files, although Delphi can combine one or more .dcu file and link to a .DLL.

    The number of .dcu files is meaningless most of the time; it's the .pas source files you need to be concerned about, and there is one of them for each source unit you create. They're re-created as needed if the source file is changed, or used in their compiled state if the source isn't changed for link speed. There is always one .dcu file per .pas file in your uses clause, plus one for your own source being compiled.

    When you install a component into the Component Palette in the IDE, the .dcu is placed in a .BPL (Borland Package Library), which is a special type of DLL; that BPL, or package, file is then loaded as executing code into the IDE. This is how a button is visually displayed on a form at designtime; the code executing in the BPL actually creates a button and allows it to be displayed and manipulated in the Form Editor.

    When you build an executable (and don't use runtime packages), the DCU file is linked into your application, and once the executable (.EXE) file is created neither the original .pas file or the .dcu is needed any longer. (Until, that is, you need to make changes to the executable, in which case the .pas file is modified, the compiler creates a new .dcu, and the linker combines them to create a new .exe.)