Attila Kovacs 629 Posted January 16, 2019 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
Attila Kovacs 629 Posted January 16, 2019 I've found {$STRONGLINKTYPES ON} and this: https://code.i-harness.com/en/q/281b33c Still curious if anyone has a different solution. Share this post Link to post
Fritzew 51 Posted January 16, 2019 Simply call [TYourClass].ClassInfo somewhere in the application or in the initialization of the implementing Unit. Should be enough 1 Share this post Link to post
Attila Kovacs 629 Posted January 16, 2019 @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
David Schwartz 426 Posted January 17, 2019 (edited) put this somewhere: TMyClass.Create().Free(); Or add a Register() method and call it in an Initialization section somewhere. Edited January 17, 2019 by David Schwartz Share this post Link to post
Uwe Raabe 2057 Posted January 17, 2019 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
David Schwartz 426 Posted January 18, 2019 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
Uwe Raabe 2057 Posted January 18, 2019 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
Stefan Glienke 2002 Posted January 24, 2019 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 1 Share this post Link to post