r/C_Programming • • 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"
#endif

Pretty 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

22 comments sorted by

View all comments

Show parent comment

0

u/c-smile Dec 14 '17

I just described every single build system in existence.

You've described exactly what modern compilers do with precompiled headers already. Therefore my question still stands. If that's possible with .h files why not with .c/.cpp ?

Instead of inventing those modules why not to add that simple [to use and understand] feature?

As of defines...

To do not create additional entities... Sources compiled in by #include source "foo.c" may use active #defines seen at point of inclusion. Thus you can pass defines from host file to child.

2

u/boredcircuits Dec 15 '17

You've described exactly what modern compilers do with precompiled headers already.

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.

Instead of inventing those modules why not to add that simple [to use and understand] feature?

#include source is 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.

To do not create additional entities... Sources compiled in by #include source "foo.c" may use active #defines seen at point of inclusion. Thus you can pass defines from host file to child.

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.