F2051: Unit %s was compiled with a different version of '%s'. Delphi error

This is a Delphi error occurs when interface part of a unit has been changed and the compiler cannot compile another unit which relies on above unit because the source is not available or not valid.

Some errors we got.

F2051 Unit System.SysUtils was compiled with a different version of "".GetMappedFileName

F2051 Unit JclUnitVersioning was compiled with a different version of System.Contnrs.TObjectList.Remove

F2051 Unit webMail was compiled with a different version of ucCodeSiteInterface

F2051 Unit ExtActns was compiled with a different version of UrlMin.IbindStatusCallBack

F2051 Unit cxClasses was compiled with a different version of Forms.TCustomForm.SetWindowState

F2051 Unit Data.DBXCommon was compiled with a different version of System.JSON.TJSONObject

For example

When we have a situation like below where Unit A uses both unit B and unit C and Unit B uses C. 


Unit B and C are compiled and for some reason the source of unit B is not available. Now Unit C is changed and the dcu of unit C differs from the unit C used by unit A, so unit A needs to be recompiled too. But unfortunately, the source is not available so the compiler gives up and shows "F2051: Unit %s was compiled with a different version of %s." error.

Why we get this error ?

This error may occur

1. When we made changes to the interface section of the unit. We cannot do this without also re-compiling all units that use the unit you are modifying

2. When a unit in your project has the same name as a standard Delphi unit

3. You either have old dcu files lying around that need to be rebuilt, or removed, or your project or library path contents are not consistent.

4. When we compile a project written in a old version of Delphi into a new Delphi version and new Delphi version don’t not have a unit name we are using.

5. If we have done any wrong modifications in Delphi Library Paths

6. When Delphi settings on the IDE or Project level contain references to multiple versions of the same library.

7. When a project have access to both directory that is in Tools -> Library Path or Project -> Option -> Search Path and both path contains same named pas or dcu files.

8. When we re-compile the unit with different compiler options and different platforms like win32, win64 from those used to build it originally.

What is the solution ?

As there are many possible reasons of occurring this error, so there are many solutions also.

The solutions are.

1. Open the unit having this error and save with new name. Change all the references to new name. Delete existing pas or dcu files with old name. Rebuild the project again.

2. Search any .dcu/.dcp/.bpl files that showing in error and then Delete all searched files. Then rebuild the project or package file.

3. Review both Tools -> Library Path and Project -> Option -> Search Path to see that both contain only required folders and should not have duplicate pas / dcu files.

4. Please check the source code file path having the error. If it does not exist in Project search path then please add to the path list.

5. Search for any duplicates of the problem unit and delete old ones or move them to backup folders where they cannot be found by Delphi.

6. For 3rd party components like Devexpress, TMS, Woll2woll, please un-install and re-install again. Before install make sure no linked dcu files exist and add the installed path to either Tools -> Library Path and Project -> Option -> Search Path.

Solutions for some of third party components 

Devexpress

For Devexpress components when we get this type error please do the following:

1) Please remove all products using our Installer (run it in Remove mode);

2) Run the following commands for every hard drive:
dir X:\cx* /s >c:\CXResult_X.txt
dir X:\dx* /s >c:\DXResult_X.txt
dir X:\dclcx* /s >c:\DCLCXResult_X.txt
dir X:\dcldx* /s >c:\DCLDXResult_X.txt
where X - drive name.

3) Check the results of these commands, and manually delete all our libraries and source files, if found.

4) Install our products again.

5) Make sure that there is a DXVCL variable in the PATH system environment variable, and it refers to a correct folder.



Comments

  1. Very thorough and clearly written. Thank you. #5 was the solution in my case.

    ReplyDelete

Post a Comment

Popular posts from this blog

ShellExecute in Delphi

MS Excel Automation in Delphi

Drawing Shapes in Delphi