Alternatives for gmake?
I have a c++ program file with two functions in it. If I change the first function alone, why should both of them have to be recompiled? Is there any build system 开发者_开发知识库which recompiles the first one alone and put it back in the same object file? Is this possible? The instructions of one function shouldn't depend on other right? Since gmake recompiles the whole file, it takes a lot of time, cant this be avoided? Putting the second function in a separate file is not a good idea, as it involves creation of unwanted files which is not necessary.
If the second function is quite long or requires more time to compile, place it in a separate file. That is why people separate source files. From what I know, it has to compile the whole file, as a small change in the source will result in a major change in the output file, as the functions would not link to each other.
I doubt that compiling only part of a source file is ever possible, using any programming language. Compilations are done on a per-file basis.
The analysis to decide which semantic parts of a given source file have changed and thus need recompiling would likely outweigh the cost of the compilation itself in most cases.
Build systems get big wins by analyzing the dependencies between source files because the cost of file I/O (particularly for include files) is a large part of the overall compilation cost. Once you've decided to recompile a given source file, you would likely only achieve a tiny speedup by ignoring unchanged parts of the file, even if there were zero cost to computing which parts those were.
All build systems for C++ that I know work on translation unit (file) level, not on function level. Although in theory it should be possible it is complicated when you consider the preprocessor, e.g.
#define ANSWER 42
void foo()
{
#undef ANSWER
#define ANSWER 41
}
int bar()
{
return ANSWER;
}
Although this is a terrible code any standard compliant compiler/build system should support it. And as you can see changing foo
(redefining ANSWER
) can affect bar
.
Putting the second function in a separate file is a good idea, and is necessary if you want to avoid this "problem". If your functions are so large that the time spent recompiling one file is noticeable, then the file is probably too big and should be broken up anyway.
The issue isn't gmake, it's the compiler. If you change one function, you may have no choice but to recompile others. For instance:
- if function
a
calls functionb
, and you change functionb
, you need to ensure that thea
still callsb
correctly, in caseb
's signature changed. - if function
b
is betweena
andc
in the memory, and nowb
grows so that it no longer fits, you may have to move eithera
orc
, which also involves recompiling to generate correct offsets. - If
b
is no longer in the same place, you need to compile its caller,a
to point to the right function.
There are probably more and better cases where this is necessary.
精彩评论