2

I have just one question related to Linux shared library files.

I saw lot of links related to dynamically shared library for the Linux O.S http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html

Here in above link it is mentioned --- include file for the library: ctest.h

Now in LINUX to use the libdl in build function --- dlopen, dlsym, dlclose.
Do we really need to include the prototype file -- ctest.h -- for dynamic lybrary ?

Please give some suggestion related to above post.

bensiu
  • 24,660
  • 56
  • 77
  • 117
user1870619
  • 177
  • 1
  • 6
  • 12

1 Answers1

1

You don't really need to include the header or prototype file for the dynamic library, you do however need to at least the specific type information for the value returned by dlsym.

See here and here for examples that don't contain the include file for the dynamic library.

In the example you posted, they started off with their library functions not having header files / function prototypes, which along with providing instruction on how to avoid C++ name mangling is why they included the header file in this case.

If you define your own libraries without function prototypes, either in the source file or the header file, then you will need to include the header file when using dlsym, otherwise the inclusion of the header file of the dynamic library is unnecessary as its function prototypes were already included in the generated shared object.

The function prototypes included in header file are so that functions implemented can be resolved by name by linker. Where as the shared object file regardless of how it is linked, contains the implementation of the library which the linker links to.

The short explanation is that header files that are included with the #include are processed by the preprocessor, which means the resulting source file / files passed to the linker has knowledge of who each function call is because it looks up the function call prototype that was in the include file and has been included in the modified source. Include files tell the linker about who the function call is.

Object files, Shared Object files and other libraries files tell either the linker about what the implementation of the function call prototype does.

To answer your question in the comment, you will only have to add the libdl.so path to LD_LIBRARY_PATH or to /etc/ld.so.conf and run ldconfig, if that library or its relevant symlink hierarchy isn't in a standard location such as /usr/lib/ or /lib/.

See the following relevant StackOverflow answer answer for more information.

Further information can be found at

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Appleman1234
  • 15,946
  • 45
  • 67
  • thanks appleman1234 . just one more thing to use --- the libdl in build function --- dlopen, dlsym, dlclose. ----- do we need to add following path for the loader ... LD_LIBRARY_PATH or ldconfig ? – user1870619 Dec 03 '12 at 07:55