by brundolf · 4y ago · 0 comments
I'm sure I'm missing something, but I'm curious why the compiler can't parse the header just once and keep it in memory (with further dependencies forming a graph of ASTs referencing each other), instead of naively combining all the raw text every time before it can parse the entire combined file?
For instance, if the file was this:
// my-file.h
#ifndef MY_FILE_INCLUDE_GUARD
#define MY_FILE_INCLUDE_GUARD
void f(void);
#endif
the compiler would remember the “#ifndef MY_FILE_INCLUDE_GUARD” part.If I ever did another C or C++ project (which would probably only be at gun–point), I would go the opposite route and have only a single compilation unit. I would have a single primary source file that included all of the others, and none of the others would include anything. Then I could run the compiler a single time on just that one file and it would be as fast and as simple as possible. Well, I don’t know of any C or C++ compilers that can spread their work over multiple cpus (since we usually just run multiple instances simultaneously with make -j), but other than that it would be as fast as possible.