Sign in Join
Wiki copytemporary

Unit not found - How to find units

Deutsch · English · Français · Magyar

Overview

This page is about the most dreaded, prominent error of Pascal: unit not found or can't find unit.

This error has a lot of potential reasons, so this page attempts to gather most of those, plus how to identify which particular problem it is and how to hopefully solve it. Usually, under this condition, Lazarus does not compile any program at all nor does it recompile the IDE any longer, meaning Lazarus is completely "broken" until the fix is applied.

Note: Lazarus provides a tool to test for many common misconfigurations: The button Project Options / Test. Click these and see the warnings it produces. Normally it will only warn (safely ignore those) about certain duplicate ppu files in the fpc sources (the actual file dupes depend on the fpc version and distribution).

Quick list

For experts here is a list of possible causes for the error. This list is not sorted for probability.

Notations, terms

Step 1: Compiler or IDE or both

The IDE and the compiler do not use the exact same way to locate units.

Compiler

If the compiler can not find it, the compilation fails (Build, Ctrl+F9) with the error 'unit not found'.

IDE

If the IDE can not find, the codetools will fail. For example Find Declaration (Alt+Up or Ctrl+Left mouse button) or Identifier completion (Ctrl+Space) will fail with the error 'unit not found'. If the error message is about units you don't use directly, then simply add the unit name to a uses section and do a find declaration. After that you can remove it.

Search strategies

What both search

 1. Unit1.pp
 2. unit1.pp
 3. UNIT1.PP
 4. Unit1.pas
 5. unit1.pas
 6. UNIT1.PAS

They are searched in every directory of the search path.

Differences between IDE and FPC unit search

Case 1: Both can not find a unit

Case 2: Compiler finds a unit, but IDE does not

The next sub cases assumes, that Run -> Build (Ctrl+F9) in the Lazarus IDE works, but Find Declaration fails with the error: unit not found. If unsure, check Case 1 as well.

Case 3: IDE finds a unit, but compiler does not

This is the case, when Find Declaration finds the unit, but building (Ctrl+F9) fails.

There are two cases: Either the compiler didn't find anything, or it does not like, what it found. In both cases it returns the same error: unit xyz not found.

You can see where the compiler sarches and what it finds by adding the -vu -vt options (short: -vut). Beware: This gives easily thousands of lines of output. The -vut must be applied to the step, that creates the error. If it happens when compiling the project, enable the -vt in the compiler options of the project. If it happens when building a package, use the compiler options of the package. If it happens when building the IDE, use the Tools / Configure Build Lazarus / Advanced Build Options / Options. Then compile again. It will slow down the compilation. Then right click on on messages window and click on 'Copy all and hidden messages to clipboard'. Then paste it i the source editor. Now search for all places where the unit name is mentioned.

Case 3.1: The compiler finds the .ppu file, but does not like it

This can happen, when the found .ppu file is older than a required unit (.pas, .pp or .ppu). For example if unit1 uses unit2 and unit2 was changed, then unit1 needs to be recompiled as well.

It can also happen, if you installed newer files without deleting the old ones. The compiler stops at the first found.

You can see, what the compiler finds and what is missing by giving the -vt command line switch. But this gives a lot of output and it is hard to read for newbies.

The easiest way is often to first recompile the packages and project clean (Build all). In case of the Lazarus sources itself: The configure build lazarus dialog has a button to setup all options for a clean build.

Case 3.2: The compiler cannot find source (.pas, .pp) or .ppu file

In this case you have to extend the search path. The question is: add the .ppu directory or the source directory?

Warning: If you add the wrong search path, it can happen, that the compiler will create multiple copies of files and you will later get the 'unit not found' error of Case 3.1.

Many projects and packages have distinct source directories (.pas, .pp) and output directories (.ppu). If a package has an output directory, then only this directory should be added to the search path of any depending project and/or package. The IDE will do this automatically. For example: Adding the LCL as dependency to your project adds two output directories to your unit search path. You can see this in Compiler Options -> Inherited. Never add any other LCL directory to your search paths or you will get Case 3.1 errors.

Case 3.3: The wrong compiler is being used

Perhaps when cross compiling the correct cross .ppu is found, but the wrong compiler is used by lazarus (see install on Linux cheatsheet).

Lazarus uses the compiler specificied in the environment options. It is easy to forget to change the compiler, if you change the target CPU. Therefore it is recommended to set the compiler in the environment options to the fpc executable, which is a front end to the ppcXXX compiler backends. The fpc executable chooses the right ppcXXX compiler based on the -P parameter or the native pccXXX compiler if no such parameter is given. The drawback of using fpc instead of for example ppc386 directly, is that an extra process needs to be started (one for fpc and one for ppcXXX) which slows down compilation slightly, but this is hardly measurable.

Run lazarus from a text console (with startlazarus) so you an see the debugging output and look at the output generated when you compile.

It may look like this:

TPkgManager.GetWritablePkgOutputDirectory APackage=DBFLaz 0.1.1 AnOutDirectory="/home/sam/.lazarus/lib/DBFLaz/powerpc-linux"
TLazPackageGraph.CheckIfPackageNeedsCompilation  No state file for JPEGForLazarus 1.0.1
[TExternalToolList.Run] CmdLine="/usr/bin/ppc386  -S2cgi -OG1 -Ppowerpc -gl -vewnhi -l -Fu../../lcl/units/powerpc-linux/ -Fu../../lcl/units/powerpc-linux/gtk2/ -Fu../../packager/units/powerpc-linux/ -Fu. -FU/home/sam/.lazarus/lib/JPEGForLazarus/powerpc-linux/ -dLCL -dLCLgtk2 jpegforlazarus.pas"   
WorkDir="/usr/share/lazarus/components/jpeg/"
Hint: Start of reading config file /etc/fpc.cfg
Hint: End of reading config file /etc/fpc.cfg
Warning: You are using the obsolete switch -OG
Free Pascal Compiler version 2.3.1 [2007/12/10] for i386
Copyright (c) 1993-2007 by Florian Klaempfl
Target OS: Linux for i386
Compiling jpegforlazarus.pas
Compiling lazjpeg.pas
PPU Loading /usr/share/lazarus/lcl/units/powerpc-linux/intfgraphics.ppu
PPU is compiled for another processor
Fatal: Can't find unit IntfGraphics used by LazJPEG
GetMsgLineFilename Filename "lazjpeg.pas" not found.
TExternalToolList.Run Exception: /usr/share/lazarus/components/jpeg/lazjpeg.pas(1,1) Fatal: Can't find unit IntfGraphics used by LazJPEG 

The clues are these lines:

[TExternalToolList.Run] CmdLine="/usr/bin/ppc386  -S2cgi -OG1 -Ppowerpc -gl -vewnhi -l -Fu../../lcl/units/powerpc-linux/ -Fu../../lcl/units/powerpc-linux/gtk2/ -Fu../../packager/units/powerpc-linux/ -Fu. -FU/home/sam/.lazarus/lib/JPEGForLazarus/powerpc-linux/ -dLCL -dLCLgtk2 jpegforlazarus.pas"   
...
Target OS: Linux for i386
...
PPU Loading /usr/share/lazarus/lcl/units/powerpc-linux/intfgraphics.ppu
PPU is compiled for another processor

Which suggest in this case that ppc386 is set in the Environment menu instead of fpc

Case 3.4: make , apt or fpcUPdeluxe installed a 2nd compiler fpc into $PATH

see install on Linux cheatsheet how to move the 2nd fpc out of the $PATH . Click "project options / Test" for diagnosis of this condition.


From the Free Pascal wiki, page "Unit not found - How to find units", as it stood on 2020-11-01 after a change by Unxed, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.