r/cpp • u/c-smile • Mar 11 '19
#include source "file.cpp", why not?
What if together with #include "file.cpp" the language would have something like #include source "file.cpp" ?
#include source "file.cpp"Includes the file as a separate compilation unit rather than just inserting it in place as in case of existing #include.
Having such #include we may include libraries as simple as this:
#include source "png.c"
// where png.c file is just a list of all files from libpng:
#include source "pngerror.c"
#include source "pngpriv.c"
#include source "pnggamma.c"
...This simple feature may eliminate the need of make, cmake, and pretty much all others from the zoo of makes. #include source and exiting C/C++ preprocessor should be enough in 90% of make cases.
Yet I think #include source can be used as a modules alternative.
Yet it is backward compatible - pretty much any exiting C/C++ library can be included that way in final executable without modification of library sources.
4
u/yehezkelshb Mar 11 '19
If I understand you correctly, you are talking about including the object file of the "included" source in the final executable, i.e. giving it as an input to the linker, not the compiler. This is supported by MSVC with
#pragma comment(lib, "libname")And it doesn't remove the need for a proper build system, as other commenters mentioned already, as we need to handle dependencies, know when to invoke the compiler on a specific source file and when the previous compilation output can be reused etc.