Number Class in a Statically Linked Library
Learn to create a class stored in a static library.
Problem
Rewrite program in “Number Class with Methods defined Inside it” so that the Number class is stored in a static library.
Solution
Here is a solution to the above problem. You have to press the “Run” button to get the terminal window under the code widget. After that, you will be able to create a static library using the given commands.
Create static library using command-line
If you wish to build the project using the command-line, carry out the following steps:
-
Compile
Number.cppto create the object file.g++ -c Number.cpp -o Number.o -
Group the
Number.oobject file in a static librarylib_NumberStaticLib.awith.aextension. Now this static library is ready to use.ar rcs lib_NumberStaticLib.a Number.o -
Let’s compile
main.cppto create its object file.g++ -c main.cpp -o main.o -
In this step, we’ll link our
main.oobject file with our static library using the given command. This would have created the executablemain.outfile.g++ -o main.out main.o -L. -l_NumberStaticLib -
Execute it using the following command:
./main.out