Adding Shared Library ======================= In this section, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is shared library ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to create shared library ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to link shared library ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Adding Shared Library : FAQs ` * :ref:`Adding Shared Library : Shared Library without Makefile ` * :ref:`Adding Shared Library : Shared Library with Makefile ` .. _linux_x86_c_add_shared_lib_0_1: .. tab-set:: .. tab-item:: Adding Shared Library : FAQs .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us answer few basic questions about shared 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 Shared Library ? .. dropdown:: See Answer * Collection of compiled object code that is linked to the source code only at the runtime. .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to link the object files to generate the shared library ? .. dropdown:: See Answer * object files should be generated with -fPIC option in gcc command * -shared option in gcc is used to generate the static library. .. code-block :: c gcc -fPIC file.c file.o gcc -shared -o mysharedlib.so file.o .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to link the shared library with the source code ? .. dropdown:: See Answer * set LD_LIBRARY_PATH to your shared library path and run the executable binary. .. code-block:: c export LD_LIBRARY_PATH=:$LD_LIBRARY_PATH ./a.out .. 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 -D libmathfunc.so w __cxa_finalize w __gmon_start__ w _ITM_deregisterTMCloneTable w _ITM_registerTMCloneTable 0000000000001127 T mul 0000000000001111 T sub 00000000000010f9 T sum * -D option is used to display dynamic symbols. .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What objdump command does ? .. dropdown:: See Answer * objdump is used to provide detailed information about object files and libraries. .. code-block:: c $ objdump -T libmathfunc.so libmathfunc.so: file format elf64-x86-64 DYNAMIC SYMBOL TABLE: 0000000000000000 w D *UND* 0000000000000000 __cxa_finalize 0000000000000000 w D *UND* 0000000000000000 _ITM_registerTMCloneTable 0000000000000000 w D *UND* 0000000000000000 _ITM_deregisterTMCloneTable 0000000000000000 w D *UND* 0000000000000000 __gmon_start__ 0000000000001127 g DF .text 0000000000000017 mul 00000000000010f9 g DF .text 0000000000000018 sum 0000000000001111 g DF .text 0000000000000016 sub * -T option is used to display dynamic symbol table. .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What readelf command does ? .. dropdown:: See Answer * readelf is used for displaying information about ELF (Executable and Linkable Format) format binaries. .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What file command does ? .. dropdown:: See Answer * file is used for providing basic information about the file including whether it is a shared library. .. code-block:: c $ file libmathfunc.so libmathfunc.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=39a0f0d0a3ff4477eb4fc706a0b85ad38f4501f8, not stripped .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us now explore it in depth ! .. _linux_x86_c_add_shared_lib_0_2: .. tab-set:: .. tab-item:: Adding Shared Library Without Makefile .. tab-set:: .. tab-item:: Creating Function Prototype * Step 1: Create an header file with all the necessary function prototypes. .. literalinclude:: shared_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:: shared_lib_ex/add.c :language: c :linenos: .. tab-set:: .. tab-item:: sub.c .. literalinclude:: shared_lib_ex/sub.c :language: c :linenos: .. tab-set:: .. tab-item:: mul.c .. literalinclude:: shared_lib_ex/mul.c :language: c :linenos: .. tab-set:: .. tab-item:: Generating Shared Library * Step 3: Generate Shared Library. .. code-block:: c $ gcc -c -fPIC add.c $ gcc -c -fPIC sub.c $ gcc -c -fPIC mul.c $ gcc -shared -o libmathfunc.so 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:: shared_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 export LD_LIBRARY_PATH=$(PWD):$LD_LIBRARY_PATH 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_shared_lib_0_3: .. tab-set:: .. tab-item:: Adding Shared Library : Shared 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:: shared_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:: shared_lib_mk_ex/src/add.c :language: c :linenos: .. tab-set:: .. tab-item:: sub.c .. literalinclude:: shared_lib_mk_ex/src/sub.c :language: c :linenos: .. tab-set:: .. tab-item:: mul.c .. literalinclude:: shared_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:: shared_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.so obj/ gcc -Wall -o app src/app.c -L./obj -lmathfunc -I./hdr mv app obj .. 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_static_lib/add_static_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_static_lib/add_static_lib` * 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`