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 15 '17
Precompiled headers aren't anything so fancy. The compiler is completely dependent on the build system to track dependencies and recompile the header, and that process is stupidly simple: just dump the compiler's state after preprocessing and parsing a header file to AST. The compiler can load this up like a checkpoint and proceed to compile the next file.
But you're right: if we can do this with headers, we can do it with compilation units. Because we already do: when the compiler has done everything it can with a single source file, it writes out the results in the object file to be linked later. Of course, the compiler is still dependent on the build system just like it is for precompiled headers.
#include sourceis easy to use and understand ... but it's not nearly enough to solve what C++ modules are trying to do. And even those aren't trying to replace the build system at all. In fact, the C++ committee has been very explicit about not making the compiler do the work of the build system with this feature. Also, you might want to look into the more recent versions of C++ modules instead of Clang's version, which won't be standardized.That's an interesting benefit. In fact, I could see implementing this with a feature somewhat similar to precompiled headers: take a snaphot of the current state before compiling the child source file, and then restoring that state when you're done. That would allow parent macros and definitions to flow to a child, without contaminating between children. I'm not sure allowing children to see macros defined in the host is a good thing, I'll have to ponder on that.