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.cppThis 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.oThis 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.cppWe will need to perform the following steps.
1. Compile the HelloWorld.cpp
g++ -Wall -c -Iinclude HelloWorld.cppThe -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.cppThe -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.oThis 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.cppBefore 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../include2. Create a static library (libhello.a).
ar -crv libhello.a HelloWorld.o3. 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 -IincludeThis 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 -IincludeAlternatively, this statement can also be used.
g++ -Wall -o Main Main.o -Llib -lhello -IincludeHere, 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