1

On Windows this can be done (though not recommended since pass c standard library object between different c library instances can have problem), like this:

Every executable image (EXE or DLL) can have its own statically linked CRT, or can dynamically link to a CRT. The version of the CRT statically included in or dynamically loaded by a particular image depends on the version of the tools and libraries it was built with. A single process may load multiple EXE and DLL images, each with its own CRT

Can this be done on Linux?

Does this mean false? But a general system like Linux should not have such restrictions right? For example, what if codebase A and codebase B really need different version of libc to work well and suppose they both have very simple C style API for the clients (i.e. no pointer parameter in those APIs)?

If it is not possible, then there is no need to read the following.

As a first step toward this goal, when I tried to build a shared library with static linked libc:

 g++ -fPIC -Wall -fexceptions -g  -c main.cpp -o main.o
 g++ -shared -static  main.o  -o libtestCppSharedLib.so
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginT.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a shared object
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
11
  • Statically linked libraries are included in the resulting binary, so there is no problem with two binaries statically linked to different libraries. Interesting is the case that (as you linked) such a statically linked library has a reference to a dynamic library. AFAIK such references include the version number of the dynamic library such that the required interface is satisfied. Multiple versions of a dynamic library can exist on a Linux system. Commented Apr 8, 2021 at 6:03
  • @thebusybee But the lib considered here is a very special lib: libc. Libc have relations with the loading/initialization/syscall process, so the problem for libc may be not trivial Commented Apr 8, 2021 at 7:17
  • Sure, but if multiple versions of libc exist and work on the specific system, why should an application with references to them not work? For example, if the application references a newer version of libc and a statically linked library references an older version of libc? Commented Apr 8, 2021 at 7:25
  • 1
    I can think of some special case that needs a specific sequence of systems calls made by the application and the statically linked library, which might not work because the different referenced versions of libc are not compatible. In this case you need to dynamically link the application to the libc that the library references, I think. Commented Apr 8, 2021 at 7:25
  • Please tell why you want to do so. It is against every usual Linux practice. Read Drepper's paper How to write shared libraries Commented Apr 8, 2021 at 17:04

1 Answer 1

3

Can this be done on Linux?

In theory: yes. In practice: no.

None of libc versions available on Linux are designed to support having multiple copies of the library in a single process.

In addition, UNIX and Linux use very different linking model, compared to Windows DLLs.

Under Windows, a DLL is a self-contained unit -- only functions and variables explicitly exported from it are visible from outside, and calling a function defined in the DLL will always result in calling that function (no symbol interpositioning).

Under the UNIX model, everything in a shared library is exported by default, and calling a function defined in the same library may or may not resolve to that function at runtime.

what if codebase A and codebase B really need different version of libc to work well a

The libc provides a standard interface. If A requires a specific version of libc to work well, it's broken. The UNIX philosophy is that you should fix A or B (or both).

Sign up to request clarification or add additional context in comments.

2 Comments

The conclusion may be true which I will verify later, but there may be some problem in the reasoning: (1) "None of libc ..." Commonly a library need not to be designed to work in this way to work in this way, they just work in this way in most case. They only break down on some special case - but what are the special cases for gcc - do they exist?
(2) "Under the UNIX model, everything in a shared library is exported", the visibility flags and attributes can solve the problem, for libc to be statically linked, the libc instances only serve clients in the shared library it is linked to and won't export the symbols, and the the clients in shared libraries with statically linked libc will use that libc, there is no dynamic linking problems for those clients. And the clients in the main executable and shared libraies without libc will use the shared version of libc with dynamic linking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.