0
votes

I have certain doubts:

Use case:

    A static library containing a global variable, static variable, a function 
and a class defined
    A shared library uses these and hence they are part of the same.
    A executable program also uses the same and they are part of the same. 
    The executable loads the shared library on run time
  1. What all differs between windows and Linux?
  2. Can the shared library use something from executable's static library objects?
  3. Can the executable use something from shared library's static library objects?
  4. What is the role of loader? For ex: does the static library objects in static library are shared between shared library and executable on Linux?
  5. Is there any difference if the shared libraryis loaded at runtime or load time?
1
Well there is nothing to stop you sending a pointer to a variable or function in one library to another library to use. - Neil Kirk

1 Answers

0
votes

In the library that you define your global static you can define it as follows:

static int g_i=9;

when accessing g_i from other modules then you should use extern to tell the compiler that g_i is defined in another module:

extern int g_i;

This is all Standard C and there should be no differences between the different C compilers regardless of operating systems.

A library can't reference anything in the executable since that would cause a circular reference, rather you should take the shared functionality in the executable and add it to a third library that is referenced by both the existing library and the executable.

There are different use cases where you rather want dynamic loading over static linking. Static libraries makes your executables larger, while dynamic libraries can cause "DLL Hell". Mostly people use shared libraries when they want to share the code between different programs and use static libraries when they only are going to use the library in only one program.