The package in Delphi (i): About EXE compilation, connection and execution

Source: Internet
Author: User

First, list the issues that need to be analyzed:

What is a bag? What is an EXE? What are their differences in composition? What's the relationship between the bag and DCU? What does the DCP do? What is the relationship between these files at compile time? And how is it loaded? How to operate the package after loading? DLL can be exports, but why does Delphi help exports, but some code is used in the package exprots?

First look at the Delphi compiler process. There are two kinds of Delphi Project: Package and program, the former suffix is dpk, the latter is DPR. From the simple beginning, first to engage in DPR. According to Delphi's help documentation, a typical DPR file is structured as follows:

1 program Editor;
2
3   uses
4    Forms, {change to QForms in Linux}
5    REAbout in 'REAbout.pas' {AboutBox},
6    REMain in 'REMain.pas' {MainForm};
7
8   {$R *.res}
9
10   begin
11    Application.Title := 'Text Editor';
12    Application.CreateForm(TMainForm, MainForm);
13    Application.Run;
14   end.

10 lines to 14 lines, Begin...end is naturally the program's implementation portal. Uses part of the program to use some of the unit, this is more ambiguous, why some will use in to indicate the source code location (this part is added to the project), and some such as forms this part, but do not need? The problem seems to be getting more complicated with each unit uses the other unit. First look at the structure of the entire source code:

The first step of the compiler is to iterate through the graph, compile it for each unit if necessary, and generate the corresponding DCU. And this "necessary" question, I began to think that the use of this unit of the statement is in, and later the test found wrong. Because in the above case, UNIT3 does not indicate the path in the UNIT1 uses clause, but it still produces the corresponding DCU file correctly. Later, using Filemon to monitor file opening, the discovery process is as follows: For each node in the diagram, the compiler searches for the corresponding PAS file of the node according to the search Path-ide environment in the current directory-project the library path in this order. Do not find it again, but this time the search is the node corresponding to the DCU file.

Now that the compilation is done, each unit (that is, the Pas file) has generated the DCU file for it, and the following problem is the connection. When it comes to connections, the problem is complicated and there are two types of connections: static and dynamic. A static connection means merging all these DCU together. Thus, the invocation of one unit to another unit becomes a matter within the program. The benefits are fast and simple, and problems such as concurrent sharing are easy to handle. The disadvantage is that the target program is large, and if you want to write another program now, and UNIT3 can reuse it, then UNIT3.DCU is copied again at the time of the connection. In this way, when two programs run simultaneously, there will be two copies of Unit3 in memory, which is more wasteful. Dynamic connections that is to say, two programs when connecting, only retain the reference to UNIT3, not copy Unit3 content. At runtime, load the Unit3 into memory and make two programs public. Both DLLs and BPL are dynamically connected solutions. The problem is that the option to connect in Delphi is only project| Options|packages menu appears, "Build with runtime packages" This sentence is too vague. So we have to study it again.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.