In the masterProgramThe global variables in the dynamic link library are correct and undeniable, but this requires the dynamic library to be added when the program compiles the link, and use extern to declare the variable in the program to be used, which does not actually achieve the essential purpose of the dynamic library (to call it again when used); this is actually not a big problem, if you only use it in one process, this article mainly discusses the use of dynamic library global variables in multiple processes.
If you want to share global variables among processes by using dynamic libraries, it is unlikely that different processes will copy the global variables when accessing the dynamic library, that is, if another process tries to modify the global variable in the dynamic library, the system will copy the global variable to the process separately, this modification operation does not affect the values of variables in other processes.
Test procedure: (http://www.liuzhigong.com/download/test_linux_so.tar.gz)
Test results:
As you can see, the main process first modifies this value, and then reads the modified value from the sub-process, however, modifications in the child process do not affect the global variables in the dynamic library used in the master process. So It is not feasible to try to share global variables between processes using dynamic libraries.. Appendix: Compilation Method
Gcc-FPIC-shared testshare. C-o libtestshare. so.2.0
Gcc-O main-L/main. c libtestshare. so.2.0
./Main