How do I initialize and share .dll on Windows and .so on Linux?

Asked 2 years ago, Updated 2 years ago, 417 views

You'll find out if you try, but it's a basic thing, so I'd like to ask you how it works.

Prerequisites

  • Suppose you created a program with multiple modules in C++. Eventually, this program runs on the command line. The target operating systems are Windows and Linux.
  • There are several submodules called from this main program. On Windows, they are built as .dll and on Linux as .so.
  • Some submodules take values from environment variables and initialize their own static data. Values are abstract, so specifically, they are paths that contain fonts. So C:\Windows\Fonts is typical for Windows.
  • On Windows, if you open two DOS windows and give them different environment variables and launch each command line for this main program, the static data in the .dll submodules will be initialized separately...because dll is not shared among processes.

What do you want to ask

However, I'm not sure what will happen to Linux's .so because I don't know enough. Linux's .so (Shared Object) seems to work as a mechanism for coexisting different versions of libraries, for example, as described below.

Shared Library

A shared library is a library that is loaded when a program starts. Once the shared library is properly installed, all subsequent programs will automatically use the new shared library. In fact, it's much more flexible and sophisticated than this, because the way Linux enables shared libraries to:

  • You can update your library while still supporting programs that want to use older, non-backward compatible versions of that library.
  • When you run a specific program, you can override a specific library or even a specific function in the library
  • All of these can be done while the program is running using an existing library

However, if the same version of .so is loaded on the system by starting the main program that started in one terminal window, how does .so in this "submodule" that initializes itself by referring to the environment variable if you start the main program by giving a different terminal window?There are probably only two answers.

I would appreciate it if you could let me know what the above will be like above.

普段 I don't usually work very close to the OS, so I think the usage of the term may be inappropriate, but I appreciate your cooperation.

[Additional note: 2020-10-11]
When I ask this kind of question, after receiving comments and answers, I realize the inaccuracy of my questioning method. I was only thinking about .dll and .so specific static data. I'm very sorry to those who already answered, but I added parts that should have been written in italics and bold.

That's it

linux windows dll

2022-09-30 21:53

2 Answers

It's a generalization because Oira doesn't know what she doesn't understand.

Shared libraries share only where they can (=not rewrite)—that is, code (where x86 relates to CS)—data is not shared.Just as each thread creates a stack, each process creates a separate data area (whether the data comes from EXE or DLL/so).

-----shared.c----

int global_variable;// Data is created differently for each process
void set_func(intval) {global_variable=val;} // code is shared

static data is also part of the data, so there is no difference in the situation that each process is created separately.The idea is that data areas exist for each process (including static data areas and heap areas used by malloc()), not EXE dedicated data areas and DLL. does not have a dedicated data area and a dedicated data area for a.outlibhoge.so.
# If you use the same DLL to share data between different EXE, that's the only difference


2022-09-30 21:53

There is no special connection between the environment variable and the dynamic link library.
Handling environment variables in the process implemented as DLLs is
It is synonymous with dealing with environment variables in the process implemented as exe.

So what the questioner wants to understand is the difference between Windows and Linux environment variables.

Environment variables are basically determined by the process unit, but
For Windows, the value can also be shared within the machine and within the same user.
On Linux, in principle, you cannot share values between processes.


2022-09-30 21:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.