Difference between revisions of "Unit not found - How to find units"

From Lazarus wiki
Jump to navigationJump to search
Line 29: Line 29:
  
 
== Case 1: Both can not find a unit==
 
== Case 1: Both can not find a unit==
 +
 +
* '''Check if the unit exists''': If not, then see chapter Searching packages, projects of a unit (ToDo: write me).
 +
* '''Check if the unit is readable''': (You can open it in the IDE). A unit source does not need to be writable. If not, then fix the permissions or get a readable copy.
 +
* '''Check if the unit has a valid pascal name''': (identifier plus extension). Valid examples: unit1.pas, Bogus2.pp, _1.pas, LAZ.PAS. Wrong examples: 1.pas (starts with number), a%.pas (contains special character), unit.PAS (extension uppercase, but filename lowercase).
 +
* '''Check if directory name contains special characters'''
 +
  1. slashes / and backslashes \ are used as directory delimiter and should never be used for directory names
 +
  2. dollar $ are used for macros and many scripting languages
 +
* '''Check for right case''': If your unit name is all lowercase or all uppercase you don't need to worry. If not, then you must make sure, that '''every''' uses section in every source code uses exactly this case. For example: if your unit is named Unit1.pas, then every uses section must use Unit1. This is not so important under windows, but under any other platform (Linux, MacOSX). The IDE gives warnings when saving files with mixed case.
 +
* '''Check if the unit is in the search path'''. ToDo: write me
  
 
== Case 2: Compiler finds a unit, but IDE does not ==
 
== Case 2: Compiler finds a unit, but IDE does not ==
  
 
== Case 3: IDE finds a unit, but compiler does not ==
 
== Case 3: IDE finds a unit, but compiler does not ==

Revision as of 01:40, 28 November 2006

Overview

This page is about one of the most prominent errors of pascal: unit not found. This error has a lot of reasons and this page tries to gather most of the reasons, how to find out, which problem it is, and how to solve it.

Step 1: Compiler or IDE

The first step is to find out, if the IDE can not find the unit, or the compiler can not find it or both.

Although the compiler and the IDE are using quite similar search algorithms to find a unit, there are some differences. That's why sometimes the IDE can find a unit, which the compiler can not and vice versus. If the compiler can not find it, the compilation fails (Build, Ctrl+F9) with the error 'unit <xxx> not found'. If the IDE can not find, the code tools will fail. For example Find Declaration (Alt+Up or Ctrl+Left mouse button) or Identifier completion (Ctrl+Space) will fail with the same error 'unit <xxx> not found'.

Common:

  • Both search units in the search path.
  • Both search first for pascal sources (.pas, .pp) files. Under MacPAS mode: search for .p files too.
  • Both search for several cases: normal case, lower case, upper case. For example: if the uses section contains 'Unit1', then it searches:
 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:

  • FPC uses the fpc.cfg for additional search paths. The IDE uses them only to find files of the FPC sources. So adding a search path to the fpc.cfg is ignored by Lazarus.
  • FPC always starts in one directory and every relative filename is relative to this directory. For the Lazarus IDE each project and each package has its own base directory. So the search path of a package is relative to the package directory. The package directory is the directory of the .lpk file. The base directory of a project is the projects main source file (e.g. the .lpr file). If there is no main source file, the directory of the .lpi file is taken. Normally both are the same and the IDE has no graphical frontend to split. But by editing the .lpi file manually you can split them. At the moment there is only one prominent example for this case: the ppxxx.lpi files of the Free Pascal compiler.
  • FPC and Lazarus have different macros.
  • You can define additional source search paths for the IDE, which are not given to the compiler. (Compiler options -> Other source files)

Case 1: Both can not find a unit

  • Check if the unit exists: If not, then see chapter Searching packages, projects of a unit (ToDo: write me).
  • Check if the unit is readable: (You can open it in the IDE). A unit source does not need to be writable. If not, then fix the permissions or get a readable copy.
  • Check if the unit has a valid pascal name: (identifier plus extension). Valid examples: unit1.pas, Bogus2.pp, _1.pas, LAZ.PAS. Wrong examples: 1.pas (starts with number), a%.pas (contains special character), unit.PAS (extension uppercase, but filename lowercase).
  • Check if directory name contains special characters
 1. slashes / and backslashes \ are used as directory delimiter and should never be used for directory names
 2. dollar $ are used for macros and many scripting languages
  • Check for right case: If your unit name is all lowercase or all uppercase you don't need to worry. If not, then you must make sure, that every uses section in every source code uses exactly this case. For example: if your unit is named Unit1.pas, then every uses section must use Unit1. This is not so important under windows, but under any other platform (Linux, MacOSX). The IDE gives warnings when saving files with mixed case.
  • Check if the unit is in the search path. ToDo: write me

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

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