r/C_Programming • u/c-smile • Dec 14 '17
Question #include source "foo.c", why not?
If we would have #include source construct that includes compilation units into final assembly (rather than just headers in-place) any library can be included to a project as plain .c file:
// foo-library.c
#include "foo.h"
#include source "foo-a.c"
#include source "foo-b.c"
#if defined(PLATFORM_MACOS)
#include source "foo-mac.c"
#elif defined(PLATFORM_WINDOWS)
#include source "foo-win.c"
#else
#include source "foo-posix.c"
#endifPretty much each popular C/C++ library can be wrapped into such amalgamation files. This will make our life easier - eliminate the need of the whole zoo of build/make systems. At great extent at least.
Yes/no ?
4
Upvotes
View all comments
Show parent comment
2
u/boredcircuits Dec 14 '17 edited Dec 14 '17
Ok, I see what you're going for. This feature would also have to deal with
#define(foo-a.ccan't have macros that change the meaning of code insidefoo-b.c). And#includeand#ifdefmatter as well. Basically anything dealing with the preprocessor. It basically would make monolithic builds require less cooperation between source files.What you're proposing is very different than MSVC feature, and a lot more complex to implement. All
#pragma commentneeds to do is pass along a filename and path to the linker, but actually compiling the source files is another matter.So let's look at how we would actually implement
#include source.In order to support incremental builds, when the compiler encounters
#include sourcethe first thing it needs to do is check to see if that file or anything it includes changed from the last built. Ok, so it doesn't know anything about files it includes, so we can't do this quite yet. We have to run the preprocessor on that file. The preprocessor's state has to be reset between files so we don't contaminate macros. As we preprocess, however, we can see if the dependencies have changed, so that when we're done we know whether or not the compilation step needs to happen. Alternatively, we can do a hash on the preprocessed file, or keep a list of the dependencies ... all common methods used by various build systems.At that point, we have a preprocessed file and can go ahead and compile it if necessary. There's no reason to wait, and there's no reason to eat up memory unnecessarily. The results of this is stored off somewhere to be reused in our incremental build.
And now we can move on to the next file and do the same thing. At the end, we take all the files we compiled and link them all together. Done!
Here's the thing: I just described every single build system in existence. Visual Studio project files, Makefiles, Ant, etc. The main difference is how you specify the files to compile. After that, the compilation of each file basically has to be done in isolation no matter what, so you don't contaminate preprocessor results and so that the final compilation results for each file (including dependency information) can be stored off for incremental builds.
Your goal here was to eliminate build systems, but you just forced the compiler to have its own instead.