0

I would like to selectively compile part of an (extremely bloated and large) framework only as the elements are used.

I've had a few ideas on how I can do this, but failed to implement either properly;

  1. Use a define macro with the same name as a class or method which toggles a variable which causes that part of the framework to be included by the preprocessor. I had an idea for an implementation of this but got stuck.

  2. Find a way to have all my function defined as templates so that the compiler doesn't generate them until they are required. I'm unsure however how I can make this work in a multi-file project.

Is there a good way to do this without using a third-party compiler or add-on/tool?

kvanbere
  • 3,289
  • 3
  • 27
  • 52
  • What's the end goal of this mental exercise? At you trying to minimize library file size, or runtime memory size? – Lie Ryan Aug 12 '12 at 14:49
  • To develop a method of compiling only the functions that are used in a solution, as to prevent dead-code. – kvanbere Aug 12 '12 at 14:51
  • Dead code, by itself does no harm, so that's not an end goal. Also, any half decent compiler will remove unreachable code. – Lie Ryan Aug 12 '12 at 15:02
  • It is however wasted compilation time and added filesize. – kvanbere Aug 12 '12 at 15:05
  • Then the template solution is a worse cure than the disease.Template cannot be precompiled, it had to be recompiled the first time it's instantiated in every file, and it's impossible for the linker or the loader to share the compiled code at runtime between multiple program that uses the same library. The macro solution is also futile, there's lots of dependency that can't easily be solved. Just let the compiler remove dead code, they can do it better than you could. – Lie Ryan Aug 12 '12 at 15:14

1 Answers1

0

It's quite impossible. Even if you were to define every scrap of code as a template, they would still be compiled. Besides, why do you even have code in your codebase that you don't use? Just delete them and use source control to recover them. You're trying to solve a non-problem that doesn't have a solution.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • From the C++ specification: `They are compiled on demand, meaning that the code of a template function is not compiled until an instantiation with specific template arguments is required. At that moment, when an instantiation is required, the compiler generates a function specifically for those arguments from the template.` So presumably then, if it were possible which it doesn't seem to be, then the code wouldn't be compiled until it were included. – kvanbere Aug 12 '12 at 14:41
  • @kvanberendonck: where did you find that? – jalf Aug 12 '12 at 14:52
  • @jalf Actually, I didn't mean to write that. It's a monophrase from the C++ specs, but it is mentioned here : http://www.cplusplus.com/doc/tutorial/templates/. I did see earlier though a quote from the actual C++ specs that says exactly the same thing. sorry. – kvanbere Aug 12 '12 at 14:59
  • 1
    The C++ spec doesn't say "exactly the same thing" :) (And the precise wording is important, because this quote implies that code inside template functions is completely ignored until the template is instantiated -- which is not true) – jalf Aug 12 '12 at 15:06
  • They are not compiled on demand. That code still has to be parsed and lexed, and the first stage of two-phase lookup performed, for example. – Puppy Aug 25 '12 at 13:41