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)

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

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.