C++/Unix: Create object files, static and dynamic libraries

Initial Directory Structure:

Screen Shot 2014-02-25 at 6.17.27 PM

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:

Screen Shot 2014-02-24 at 6.51.53 PM

  • Create an archive of all the object files:  Screen Shot 2014-02-24 at 6.52.34 PMScreen Shot 2014-02-24 at 6.52.47 PM
  • 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 commandScreen Shot 2014-02-24 at 6.52.56 PM

Dynamic Library:

  • Create object files of all source code files:

Screen Shot 2014-02-24 at 6.53.12 PM

  • Create Dynamic library with all the object files we just created. Replace “-dynamiclib” with “-shared” for normal linux and unix machines.

Screen Shot 2014-02-24 at 6.54.03 PM

Screen Shot 2014-02-24 at 6.55.38 PM

Compile the Main Program:

  • Compile the hellobeatles.cpp into object file:

Screen Shot 2014-02-25 at 6.04.39 PM

  • Link the object file with the libraries to create the executable:

Screen Shot 2014-02-25 at 6.13.50 PM

Here is the list of files we finally have(highlighted with green is our executable):

Screen Shot 2014-02-25 at 6.14.55 PM

Published by

Sreejith

A strong believer of: 1. Knowledge is power 2. Progress comes from proper application of knowledge 3. Reverent attains wisdom 4. For one's own salvation, and for the welfare of the world

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s