Initial Directory Structure:
Source code:
.//georgeringo/george.cpp #include #include "george.hpp" void george() { std::cout << "George, "; } .//georgeringo/george.hpp #ifndef GEORGE_HPP_INCLUDED #define GEORGE_HPP_INCLUDED void george(); // Prints "George, " #endif // GEORGE_HPP_INCLUDED .//georgeringo/georgeringo.cpp #include "george.hpp" #include "ringo.hpp" #include "georgeringo.hpp" void georgeringo() { george(); ringo(); } .//georgeringo/georgeringo.hpp #ifndef GEORGERINGO_HPP_INCLUDED #define GEORGERINGO_HPP_INCLUDED void georgeringo(); #endif // GEORGERINGO_HPP_INCLUDED .//georgeringo/main.cpp #include "georgeringo.hpp" #include using namespace std; int main() { georgeringo(); } .//georgeringo/ringo.cpp #include #include "ringo.hpp" void ringo() { std::cout << "and Ringo\n"; } .//georgeringo/ringo.hpp #ifndef RINGO_HPP_INCLUDED #define RINGO_HPP_INCLUDED void ringo(); // Prints "and Ringo\n" #endif // RINGO_HPP_INCLUDED .//hellobeatles/hellobeatles.cpp #include "johnpaul/johnpaul.hpp" #include "georgeringo/georgeringo.hpp" int main() { // Prints "John, Paul, George, and Ringo\n" johnpaul(); georgeringo(); } .//hellobeatles/main.cpp #include "johnpaul/johnpaul.hpp" #include "georgeringo/georgeringo.hpp" #include using namespace std; int main() { georgeringo(); } .//johnpaul/john.cpp #include "john.hpp" #include void john() { std::cout << "John, "; } .//johnpaul/john.hpp #ifndef JOHN_HPP_INCLUDED #define JOHN_HPP_INCLUDED void john(); // Prints "John" #endif // JOHN_HPP_INCLUDED .//johnpaul/johnpaul.cpp #include "john.hpp" #include "paul.hpp" #include "johnpaul.hpp" void johnpaul() { john(); paul(); } .//johnpaul/johnpaul.hpp #ifndef JOHNPAUL_HPP_INCLUDED #define JOHNPAUL_HPP_INCLUDED void johnpaul(); // Prints "John, Paul, " #endif // JOHNPAUL_HPP_INCLUDED .//johnpaul/paul.cpp #include "paul.hpp" #include void paul() { std::cout << "Paul, "; } .//johnpaul/paul.hpp #ifndef PAUL_HPP_INCLUDED #define PAUL_HPP_INCLUDED void paul(); // Prints "Paul" #endif // PAUL_HPP_INCLUDED
Static Library:
- Create object files of all source code files:
- To update the archive’s symbol table. This is taken care by “ar” in most cases. To be always on the safe side, run the below command
Dynamic Library:
- Create object files of all source code files:
- Create Dynamic library with all the object files we just created. Replace “-dynamiclib” with “-shared” for normal linux and unix machines.
Compile the Main Program:
- Compile the hellobeatles.cpp into object file:
- Link the object file with the libraries to create the executable:
Here is the list of files we finally have(highlighted with green is our executable):