Library of your own – object file.

Sometimes you want to have a library of your functions within one object file that you can use for other applications without having to re-compile and import each time to each project.

Well, if you create a object of the list of functions because a object file is the intermediate between code and linking (to create the executable file), but with this object file you will need to have created a header file (.h) that will allow the projects to know what functions are within the object file and how to call them. So to start with here is the header file (I have called it libraryHelloWorld.h)

#include <iostream>
using namespace std;
void SayHelloWorld();
void SayWord(std::string st);

It just defines the two functions that I have written, and here are the implementation of the two functions above.

#include "libraryHelloWorld.h"
#include <iostream>
using namespace std;
void SayHelloWorld()
    cout << "Hello World" << endl;
void SayWord(string st)
    cout << "Line is \"" << st << "\"" << endl;

if you save that as libraryHelloWorld.cpp, if you notice that at the top I am including the header file to this cpp file because the header files normally have a class definition inside them and also struct’s etc, which the functions within the cpp file may require.

To compile up, to create the object file you just need to do

g++ libraryHelloWorld.cpp -c

which basically means (-c) just compile and do not link, this will create a libraryHelloWorld.o file (the object file).

To make use of this object file, you just need to add in the header file to your project and then call the functions as though you have re-written them within your new project, like so.

#include "libraryHelloWorld.h"
int main()
  SayWord("Genux is great");
  return 0;

Then the main part, which is including the object file created before into the linking part of the computation of this program. (save the above as callHelloWorld.cpp)

g++ callHelloWorld.cpp libraryHelloWorld.o -o callHelloWorld

the above includes the object file, libraryHelloWorld and after the linking part of the compilers job, the output file name (-o) will be callHelloWorld.

This just saves allot of time when you are coding, try to keep different projects having similar libraries that you know and trust to work.

Xml object – c++

From the main project xmlreader, here is the xml object that I created to have a xml line of code which holds the details of a basic xml.

The main basics of a xml are as below

<tagname attributesname="attributesvalue">value</tagname>

So I will need to store the tagname, attributes and the value. Here is the class structure that I did come up with.

class xmlObject {
  private :
    string _tagName, _tagValue;
    vector<xmlAttribute> _attributes;
    bool _xmlMainDetails;
      xmlObject() { _xmlMainDetails = false;} ;
      xmlObject(string tag, string tValue, xmlAttribute attribute);
      void setTagName(string tagName);
      void setTagValue(string tagValue);
      void addAttributes(xmlAttribute attribute);
      void setAttributeVector(vector<xmlAttribute> setAtt);
      void setXmlMainDetails(bool value);
      bool getXmlMainDetails();
      void printOutXmlObject();

The set/getXmlMainDetails are if the are at the top of the xml file and need to store them in a different place.

A vector is a nice array basically, it allows to dynamically increment the size of the array with using the push_back (and the opposite to shrink pop_back).

The basics of a vector are as below, means to have a vector of type int

vector<int> intvector;

Here is the class implementation of the object structure xmlObject

/* xmlObject */
// constructor for xmlObject, if any details are passed whilst constructing 
xmlObject::xmlObject(string tag, string tValue, xmlAttribute attribute)
  _tagName = tag;
  _tagValue = tValue;
// xml <tagname attributes="attributesvalue">VALUE</tagname>
// set the tag name
void xmlObject::setTagName(string tagName)
  _tagName = tagName;
// set tag value
void xmlObject::setTagValue(string tagValue)
  _tagValue = tagValue;
// add attributes to the vector attributes variable
void xmlObject::addAttributes(xmlAttribute attribute)
// fill in the vector attributes variable.
void xmlObject::setAttributeVector(vector<xmlAttribute> setAtt)
  _attributes = setAtt;
// print out the xml object detais, with the attributes values.
void xmlObject::printOutXmlObject()
  cout << "XML Object" << endl;
  cout << "Tagname  :" << _tagName << endl;
  cout << "Tagvalue :" << _tagValue << endl;
  for (int i= 0; i < (int)_attributes.size(); i++)
      cout << "Attribute " << i << " : Name : "<< << " Value : " << << endl;
// set the main set details value
void xmlObject::setXmlMainDetails(bool value)
    _xmlMainDetails = value;
// get a boolean value to see if the xmlObject is the main <?xml .. ?> value
bool xmlObject::getXmlMainDetails()
  return _xmlMainDetails;

I shall post on how to implement/compile etc a class in two different files later on, in a lessons basics for different languages but on the whole, if you store the top structure in a .h header file and then the implementation in a .cpp file. Of course shall post the whole code to store in .h .cpp files accordlying for the whole project but this is just a stripped down version.