Jens Mühlenhoff
Jens Mühlenhoff

Reputation: 14883

Why does the compiler place DCUs that do not belong to the current package in its unit output directory?

I created two packages, P1.dproj and P2.dproj.

I put two empty units into the packages, so that P1 contains Unit1.pas and P2 contains Unit2.pas.

I edited the unit output directory to .\P1\$(Platform)\$(Config) and .\P2\$(Platform)\$(Config) in the respective packages.

I added P1 as a reference to the P2 project, so that P2 depends on P1.

The project files are stored in the same folder.

The directory structure looks like this:

  Root\
    Source\
      P1\
        Unit1.pas
      P2\
        Unit2.pas
    Packages\
      P1.dpk
      P1.dproj
      P2.dpk
      P2.dproj
      P1\
        Win32\
          Debug\
      P2\
        Win32\
          Debug\

Before I added the dependency P1 was outputting a Packages\P1\Win32\Debug\Unit1.dcu and P2 was outputting a Packages\P21\Win32\Debug\Unit2.dcu.

Nothing unexpected.

After I added the dependency when I just build P2 the IDE automatically compiles P1 as well, but the Unit1.dcu file is now outputted into the Packages\P2\Win32\Debug\Unit1.dcu.

Why is that?

The Unit1.dcu file is not in the Contains list of P2.droj nor is it listed in any library path or browsing path (neither Delphi global nor project local).

Why does the compiler put stuff into the unit output directory of the current project that it does not even have access to the source directory of?

When I move the P1 dpk/dproj files away into some sub folder and restart the IDE it will correctly complain that it can not find the P1.dcp file and not start to compile anything from P1.

I had noticed this behaviour in a bigger, more complicated setup and it was driving me crazy ...

Upvotes: 0

Views: 258

Answers (1)

Dave Olson
Dave Olson

Reputation: 1494

It's doing this because the source files for both packages are in the same directory and P1 is set for "Rebuild as needed" in it's Project options.

Think about a simple project Proj1 that contains and uses a unit U1. U1 uses U2 which is not included in Proj1. The compiler must be able to see U2.dcu; probably in the current Library path or some other common "lib" directory. When Proj1 is compiled, U1.dcu goes into Proj1's unit output directory but U2.dcu is simply "used" from wherever it currently lives and does not appear in Proj1's unit output directory.

Now, suppose you add the directory where U2.pas exists to Proj1's search path. Now the compiler can see U2.pas, so it compiles it and places the resulting U2.dcu into Proj1's unit output directory along with U1.dcu

It's the same thing here...P2 "requires" P1. That is, when compiling P2, the compiler needs to see either P1.DCP or all of the source files for P1 (P1.dpk and unit1.pas in this case).

In your case, it can see P1.dpk because it's in the same directory, so it compiles it and places the resulting dcu's (unit1.dcu in this case) into P2's unit output directory.

You can prevent this behavior by setting P1 to "Explicit Rebuild" in it's project options. When building P2 or any package that "requires" P1, this tells the compiler "even though you can see P1's source, do not build it."

Upvotes: 1

Related Questions