Adding Static Library ======================= In this section, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is static library ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to create static library ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to link static library with object files ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Adding Static Library : FAQs ` * :ref:`Adding Static Library : Static Library without Makefile ` * :ref:`Adding Static Library : Static Library with Makefile ` .. _linux_x86_c_add_static_lib_0_1: .. tab-set:: .. tab-item:: Adding Static Library : FAQs .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us answer few basic questions about static library .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is the use of library ? .. dropdown:: See Answer * It is a collection of pre-compiled functions that can be used by a program. * Used to reduce the redundancy of code. .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What are the types of library ? .. dropdown:: See Answer * Two types of library: * Static Library * Shared Library .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is Static Library ? .. dropdown:: See Answer * Collection of compiled object code that can be linked directly to the program at the compile time. .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to link the object files to generate the static library ? .. dropdown:: See Answer * ar command is used to generate the static library. .. code-block :: c ar rcs .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What command is used to link the generated static library in compile time ? .. dropdown:: See Answer * To use the static library we must use this gcc command .. code-block:: c gcc sample.c -o sample -L -l * -L specifies the directory path where the library is located. * -l links with the specified library .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow In ar command what option "t" does ? .. dropdown:: See Answer * option t is used to display the table listing of the object files linked with the static library. .. code-block:: c $ ar t libmathfunc.a add.o mul.o sub.o .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What "nm" command does ? .. dropdown:: See Answer * nm command is used to display information about symbol in objects file or binaries. .. code-block:: c $ nm libmathfunc.a add.o: 0000000000000000 T sum mul.o: 0000000000000000 T mul sub.o: 0000000000000000 T sub .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Why the size of executable file linked with static library file is greater when compared to shared library file ? .. dropdown:: See Answer * The linking of static library file happens at compile time. * This results in copying the whole static library file with all its function to the executable bin. .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us now explore it in depth ! .. _linux_x86_c_add_static_lib_0_2: .. tab-set:: .. tab-item:: Adding Static Library Without Makefile .. tab-set:: .. tab-item:: Creating Function Prototype * Step 1: Create an header file with all the necessary function prototypes. .. literalinclude:: static_lib_ex/math.h :language: c :linenos: .. tab-set:: .. tab-item:: Creating Function Definition * Step 2: Create source code file with function definitions for all the functions defined in header file. .. tab-set:: .. tab-item:: add.c .. literalinclude:: static_lib_ex/add.c :language: c :linenos: .. tab-set:: .. tab-item:: sub.c .. literalinclude:: static_lib_ex/sub.c :language: c :linenos: .. tab-set:: .. tab-item:: mul.c .. literalinclude:: static_lib_ex/mul.c :language: c :linenos: .. tab-set:: .. tab-item:: Generating Static Library * Step 3: Generate Static Library using ar command. .. code-block:: c $ gcc -c add.c $ gcc -c sub.c $ gcc -c mul.c $ ar rcs libmathfunc.a add.o sub.o mul.o .. tab-set:: .. tab-item:: Creating an Application * Step 4: Create an application and use the functions being defined in the static library file created. .. literalinclude:: static_lib_ex/app.c :language: c :linenos: .. tab-set:: .. tab-item:: Compiling the Application * Step 5: Compile the application by linking the static library with gcc command. .. code-block:: c gcc -o output app.c -L. -lmathfunc .. tab-set:: .. tab-item:: Executing the Application * Step 6: Executing the application by running the binary file. .. code-block:: c $ ./output Sum = 6 Sub = -2 Product = 8 .. _linux_x86_c_add_static_lib_0_3: .. tab-set:: .. tab-item:: Adding Static Library : Static Library With Makefile .. tab-set:: .. tab-item:: Creating Function Prototype * Step 1: Declare all the function which needs to be linked with the static library in the header file. .. literalinclude:: static_lib_mk_ex/inc/math.h :language: c :linenos: .. tab-set:: .. tab-item:: Creating Function Definition * Step 2: Create source code file with function definitions for all the functions defined in header file. .. tab-set:: .. tab-item:: add.c .. literalinclude:: static_lib_mk_ex/src/add.c :language: c :linenos: .. tab-set:: .. tab-item:: sub.c .. literalinclude:: static_lib_mk_ex/src/sub.c :language: c :linenos: .. tab-set:: .. tab-item:: mul.c .. literalinclude:: static_lib_mk_ex/src/mul.c :language: c :linenos: .. tab-set:: .. tab-item:: Creating Makefile * Step 3: Creating Makefile with all the instructions required to generate the static library. .. literalinclude:: static_lib_mk_ex/Makefile :language: c :linenos: .. tab-set:: .. tab-item:: Compiling the Application .. code-block:: c $ make gcc -Wall -Wextra -O2 -g -MM src/mul.c >src/mul.d gcc -Wall -Wextra -O2 -g -MM src/sub.c >src/sub.d gcc -Wall -Wextra -O2 -g -MM src/add.c >src/add.d gcc -Wall -Wextra -O2 -g -c -o src/add.o src/add.c gcc -Wall -Wextra -O2 -g -c -o src/sub.o src/sub.c gcc -Wall -Wextra -O2 -g -c -o src/mul.o src/mul.c gcc -c src/add.c src/sub.c src/mul.c ar -rc libmathfunc.a src/add.o src/sub.o src/mul.o mv *.o obj/ mv src/*.o obj/ mv src/*.d obj/ mv libmathfunc.a obj/ gcc -Wall -o app src/app.c -L./obj -lmathfunc -I./hdr mv app bin .. tab-set:: .. tab-item:: Executing the Application .. code-block:: c $ ./app Sum = 6 Sub = -2 Product = 8 .. card:: See Also * Other topics of linux x86 * :doc:`../add_hello_world_app/add_hello_world_app` * :doc:`../add_shared_lib/add_shared_lib` * :doc:`../add_hello_world_kernel_mod/add_hello_world_kernel_mod` * :doc:`../add_application_patch/add_application_patch` * :doc:`../add_static_lib_patch/add_static_lib_patch` * :doc:`../add_shared_lib_patch/add_shared_lib_patch` * :doc:`../add_kernel_mod_patch/add_kernel_mod_patch` * Current Module * :doc:`../add_shared_lib/add_shared_lib` * Previous Module * :doc:`../add_hello_world_app/add_hello_world_app` * Next Module * :doc:`../../../linux_openwrt_rpi/linux_openwrt_rpi_c` * Other Modules * :doc:`../../../linux_rdkb_rpi/linux_rdkb_rpi_c` * :doc:`../../../linux_yocto_rpi/linux_yocto_rpi_c`