c++ - Shared libraries and .h files -
I have some doubts how programs use shared libraries.
When I create a shared library with Shared-FPIC Switches) I provide some functions from an external program. Generally I do a dump () to load the library and then DSCIM () said that to link functions related to functions pointers. No .h file is included in this approach. Is there any way to avoid Dulappan ()? Including Dlsym () and just shared library .h?
I can guess this may be how the C ++ program uses the code stored in the system's shared library. Ie just stdlib.h and so on.
Nick, I think all other answers are actually answering your question, just like You link to the libraries, but the way your question suggests you are a misconception of the difference between header files and libraries, they are not the same. You need both , and they are not doing the same thing.
Creating an executable consists of two main steps, the compilation (which changes your source into an intermediate form, including executable binary instructions, but not a runnable program), and linking (which includes these intermediate files To a single movable or libraries).
When you do gcc -c program.c
, you're compiling, and you generate program.o
. This is the step, where headers case will give you the #include & lt; Stdlib.h & gt;
(for example) program.c
is required in malloc
and free
. (Similarly you need #include & lt; dlfcn.h & gt;
for dlopen
and dlsym
) if you do not , The compiler will complain that it does not know what these names are, and stop with an error but if you do the #include
headers, then the compiler not Inserts the code that you can call program.o
. It only inserts them to context because the code is to avoid the duplication of the code: the code is being transmitted only to every part of your program, so if you have further files ( Module1.c
, if module2 is needed then C
even more), even if they all used malloc
you only A copy of malloc . This single copy is present in the standard library in either the shared or static form (
libc.so
or libc.a
) but these are not Referenced in your source, and the compiler is not aware of them.
The linker is . In the link phase, you do the GCC-O program program.o
. The linker will then search for all the libraries which you pass it on the command line and find the single definition of all the functions that you have not defined in your code, this is what -l
(as explained by others): Linker needs to use the list of libraries you want to use. Their names do not have to do anything with the headers used in the previous step, for example, to use dlsym
, you would see the libdl.so
or libdl .a
is required, so your command-line gcc - o program program.o - wdl
. To use malloc
or std * .h
, you need libc
for most functions but because this library Every C program is the auto link (as you did -lc
).
Sorry, if I have a lot in detail but if you do not know the difference you want
One last thing: Duplication
and < Code> dlsym are not common methods, it is difficult to understand how the compilation works. The linking is used for those special cases, where you want to dynamically determine on which basis you want based on that information, for some reason, is available only on runtime if you know What time do you want to call blanket timing (99% true), you do not need to use the dl *
function.
Comments
Post a Comment