Ben
Ben

Reputation: 3440

Compile/Link unused Functions/Procedures in Delphi

I would like to "provide" several functions/procedures or even vars in my application without ever using them in my own code. Does the compiler automatically ignore unused vars and functions or is it the linker? How can I change that? I already tried to uncheck CodeGeneration ---> Optimization but with no luck.

Upvotes: 2

Views: 965

Answers (3)

David Heffernan
David Heffernan

Reputation: 612993

If something in your program refers to the object then the linker cannot remove it. So you can take advantage of this like this:

procedure StopLinkerRemoval(P: Pointer);
begin
end;

Then in your initialization section you can write this:

StopLinkerRemoval(@MyVar);
StopLinkerRemoval(@MyFunction);

All you need to do is refer to the object. You don't need to call the function, or read/write the variable, just take its address.

Upvotes: 6

Sir Rufo
Sir Rufo

Reputation: 19106

To have "unused" or not referenced procedures/functions and also private and protected methods compiled in your application you should build a package and put the dcu files into your library path.

public and published methods are compiled even without being referenced in your application.

Upvotes: 4

Bogdan Doicin
Bogdan Doicin

Reputation: 2416

The linker is the one that ignores the unused variables. I am not sure if it does the same with procedures and functions, however. You cannot alter this change.

Upvotes: 1

Related Questions