Wednesday, December 1, 2010

Introduction to GCC

In this blog, I am going show you the basic stuff on how to compile, link, and create an executable of a C++ application using GCC. Although the example here uses C++, it should also be applicable to C by changing the compiler to use gcc instead of g++.

HelloWorld.hpp
#include <iostream>
using namespace std;

class HelloWorld
{
public:
    void sayHello() const;    
};

HelloWorld.cpp
#include "HelloWorld.hpp"

void HelloWorld::sayHello() const 
{
    cout << "Hello World" << endl;
}

Main.cpp
#include "HelloWorld.hpp"

int main()
{
    HelloWorld helloWorld;
    helloWorld.sayHello();        

    return 0;
}

If for example, we have a directory structure is as below.
/home/cpp/HelloWorld.cpp
         /HelloWorld.hpp
         /Main.cpp

In order to create a Main executable, we need to perform the following steps.
1. Compile the HelloWorld.cpp
g++ -Wall -c HelloWorld.cpp
-Wall option is used to display all the warnings. This will produce HelloWorld.o object file.
2. Compile the Main.cpp
g++ -Wall -c Main.cpp
This will produce Main.o object file.
3. Link the two object files (HelloWorld.o and Main.o) to create an executable (Main).
g++ -Wall -o Main Main.o HelloWorld.o
This will produce Main executable file.
4. Execute the Main file.
./Main

If we change the directory structure as below.
/home/cpp/HelloWorld.cpp
         /include/HelloWorld.hpp
         /Main.cpp
We will need to perform the following steps.
1. Compile the HelloWorld.cpp
g++ -Wall -c -Iinclude HelloWorld.cpp
The -Iinclude is used here to tell the that header files are located in the ./include directory since HelloWorld.cpp requires HelloWorld.hpp (see the source file of HelloWorld.cpp). This step will produce HelloWorld.o object file.
2. Compile the Main.cpp
g++ -Wall -c -Iinclude Main.cpp
The -Iinclude is used here to tell that the header files are located in the ./include directory since Main.cpp requires HelloWorld.hpp (see the source file of Main.cpp). This step will produce Main.o object file.
3. Link the two object files (HelloWorld.o and Main.o) to create an executable (Main).
g++ -Wall -o Main Main.o HelloWorld.o
This will produce Main executable file.
4. Execute the Main file.
./Main

To make things more complicated, let's change the directory structure as below.
/home/cpp/lib/HelloWorld.cpp
         /include/HelloWorld.hpp
         /Main.cpp
Before that, let's create a static library (libhello.a) in the lib directory.
1. Compile HelloWorld.cpp
cd lib
g++ -Wall -c HelloWorld.cpp -I../include
2. Create a static library (libhello.a).
ar -crv libhello.a HelloWorld.o
3. To view the content of the static library.
ar -t libhello.a

Now we the directory structure should look like this.
/home/cpp/lib/libhello.a
         /include/HelloWorld.hpp
         /Main.cpp

1. Compile Main.cpp
g++ -Wall -c Main.cpp -Iinclude 
This step will produce Main.o object file.
2. Link the library to create a Main executable file.
g++ -Wall -o Main Main.o lib/libhello.a -Iinclude
Alternatively, this statement can also be used.
g++ -Wall -o Main Main.o -Llib -lhello -Iinclude
Here, we don't tell where the libhello.a location explicitly. Instead, we tell where the library directory is by specifying the -Llib. This means that the library directory is located in the ./lib. The -l will strip the word lib. Thus, if we have libraries like below:
libhello.a ---> this will become -lhello
libabc.a ---> this will become -labc
libxyz.a ---> this will become -lxyz

3. Execute the Main file.
./Main

No comments:

Post a Comment