+ 1
Is there any difference between using the include directive versus externing everything?
In Cpp, is externing all of the functions and variables and later, compiling and linking both my source and the header files the *same* as including it and using its members?
6 Respuestas
+ 4
Calvin Jude your notions are all correct. Using extern indicates to the compiler that it may have to backpatch the function reference after it locates the function definition externally, but the eventual result is the same as #include of the function source or function prototype.
Using #include does simply insert the include file text into your source as part of preprocessing before compiling.
+ 3
It's been a long time since I have examined what is in C and C++ standard headers. There have been many revisions since then, so my answer here is not definitive. Standard include files contain constant definitions, macros, and prototypes for functions and C++ classes found in standard object* files and library files. The object files and libraries are already compiled, so function prototypes are all that is needed. The linker takes care of merging them into an executable, combining only object code that is actually used - not the entire libraries. A make file provides the script for which standard (and other) object files and libraries to link. It is all customizable.
*object files are not to be confused with C++ objects. It's a term that pre-dates object-oriented programming. Object files contain an intermediate form of compiled code that has all but the final address references that need to be filled in by the linker. Libraries are like many combined object files with a table of contents.
+ 3
Hi Calvin Jude
Using extern for everything and then compiling or linking might technically work, but it’s not the same, headers help enforce consistency and modularity, are safer, and reduce maintenance headaches.
0
What exactly does the #include directive do? Does it paste the header's text in the source file and then feed it to the compiler?
0
Brian I've been reading more about header files and noticed that most of them mainly contain forward declarations for functions, with their actual implementations located in separate files. Does this also apply to iostream? If so, how is the corresponding implementation file automatically compiled and linked?
0
Do all standard headers only contain prototypes?