Jump to content
Attila Kovacs

Include unused class and its RTTI in the executable

Recommended Posts

What is the simplest way to make the compiler/linker include a class and its RTTI into the executable when I have no references to the class in the code?

ATM I have an "if" with a variable in the expression which always evaluates to false and in the begin-end block a "TxyClass.Create;".

I don't really like this solution.

Share this post


Link to post

Simply call [TYourClass].ClassInfo somewhere in the application or in the initialization of the implementing Unit.
Should be enough

  • Like 1

Share this post


Link to post

@Fritzew Good point, thank you. ClassInfo not, but for example ClassParent does the trick. This one is the simplest for now. And ClassParent() is also very tiny.

Share this post


Link to post

put this somewhere:

 

TMyClass.Create().Free();

 

Or add a Register() method and call it in an Initialization section somewhere.

Edited by David Schwartz

Share this post


Link to post
7 hours ago, David Schwartz said:

put this somewhere:

 

TMyClass.Create().Free();

That is pretty similar to the original approach described in the topic. In addition, there it is wrapped in a never-true condition to avoid being executed.

Share this post


Link to post
22 hours ago, Uwe Raabe said:

That is pretty similar to the original approach described in the topic. In addition, there it is wrapped in a never-true condition to avoid being executed.

is there any guarantee that the compiler won't ever figure out the never-true condition and simple ignore the code in the block? 

 

If it's never executed then there can't be any side-effects, so why not just ignore it entirely? That would not solve the problem, would it?

 

I don't like stuff that depends on subtle compiler optimizations that can occur silently and I'd never know they happened unless I happened to look at the generated code. 

 

It's even worse than that because it would work fine in debug mode that does no optimization, but then fail when the code is built as a Release and optimization is turned on.

Share this post


Link to post

In case the compiler can evaluate that condition, that might be an issue, but there are many ways to play on the safe side there. Actually creating and freeing the class may come with some costs, though.

Share this post


Link to post

I don't see the Delphi compiler performing any control flow analysis and eliminate code in any possible future.

C++ might do that though (see https://stackoverflow.com/questions/6664471/observable-behaviour-and-compiler-freedom-to-eliminate-transform-pieces-c-co)

 

However I requested adding a way to specifically force linking of types into the binary some while ago - see https://quality.embarcadero.com/browse/RSP-18389

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×