Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to static variables when libraries are statically linked

Let's say I have library (A) implementing the singleton pattern (it has a static variable in its implementation).

(A) library is compiled as a static library.

Now, let's say I have in my probject:

  • (B), another static library linking statically with (A).
  • (C), another static library linking statically with (A).
  • (D), a top level program linking with (B) and (C).

In the end, is my singleton really a singleton (and my variable really static)? Are (B) and (C) seing the same static variable from (A) (is it unic)? Or does the fact that (A) was statically linked twice embedded (A)'s code twice ending up with my static variable from (A) appearing twice in the final binary code? Then if (B) modifies the static variable value, (C) would not see the change?

Note: I experienced that when changing the libraries of project to be linked statically instead of dynamically. I'm just wondering if I did something wrong, or if that's a normal known behaviour.

like image 864
jpo38 Avatar asked Apr 28 '16 08:04

jpo38


People also ask

What happens when you link a static library?

Static linking increases the file size of your program, and it may increase the code size in memory if other applications, or other copies of your application, are running on the system. This option forces the linker to place the library procedures your program references into the program's object file.

What does it mean to statically link a library?

In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.

Are static libraries linked?

A static library is a programming concept in which shared libraries with special functionalities, classes or resources are linked to external applications or components, facilitating the creation of stand-alone and executable files.


2 Answers

static linked library, and static variables are not related.

A static variable, is not visible outside the current compilation scope (no symbol name is created in the .o file, so no other .o files can find that variable via the symbol name).

This means that the variable

static int foo; can exist in every compilation scope, and each one will be uniqe

like image 109
Stian Skjelstad Avatar answered Oct 28 '22 04:10

Stian Skjelstad


First of all:

(B) and (C) do NOT link against (A). Static libs are compiled, not linked. When building (B) and (C) the compiler might need to see certain definitions from (A) but do not confuse this with linking. (A's) code is not copied into (B) or (C).

Secondly:

(D) will have to link against (A), (B) and (C). That means you get only one copy of (A's) code in (D).

Dynamic-link Library/Shared Object:

This of course would be different if (B) and (C) were dlls/sos instead. Dlls are linked and so if you build (B) and (C) as dlls and link them against (A) then you would have a separate copy of (A's) code in both (B) and (C).

Are (B) and (C) seing the same static variable from (A)

This depends on if your variable has external or internal linkage. The following header file contains a static int variable with interal linkage. This means that every translation unit that includes this file will get it's own copy of myVariable.

//MyHeader.h
#pragma once
static int myVariable = 0;
like image 27
Mohamad Elghawi Avatar answered Oct 28 '22 02:10

Mohamad Elghawi