Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

different behavior when linking with static library vs using object files in C++

I'm working with some legacy C++ code that is behaving in a way I don't understand. I'm using the Microsoft compiler but I've tried it with g++ (on Linux) as well—same behavior.

I have 4 files listed below. In essence, it's a registry that's keeping track of a list of members. If I compile all files and link the object files into one program, it shows the correct behavior: registry.memberRegistered is true:

>cl shell.cpp registry.cpp member.cpp
>shell.exe
1

So somehow the code in member.cpp gets executed (which I don't really understand, but OK).

However, what I want is to build a static library from registry.cpp and member.cpp, and link that against the executable built from shell.cpp. But when I do this, the code in member.cpp does not get executed and registry.memberRegistered is false:

>cl registry.cpp member.cpp  /c
>lib registry.obj member.obj -OUT:registry.lib
>cl shell.cpp registry.lib
>shell.exe
0

My questions: how come it works the first way and not the second and is there a way (e.g. compiler/linker options) to make it work with the second way?


registry.h:

class Registry {
public:

    static Registry& get_registry();
    bool memberRegistered;

private:
    Registry() {
        memberRegistered = false; 
    }
};

registry.cpp:

#include "registry.h"
Registry& Registry::get_registry() {
    static Registry registry;
    return registry;
}

member.cpp:

#include "registry.h"

int dummy() {
    Registry::get_registry().memberRegistered = true;
    return 0;
}
int x = dummy();

shell.cpp:

#include <iostream>
#include "registry.h"

class shell {
public:
    shell() {};
    void init() {
        std::cout << Registry::get_registry().memberRegistered;
    };
};
void main() {
    shell *cf = new shell;
    cf->init();
}
like image 645
MaartenB Avatar asked Jul 19 '16 23:07

MaartenB


People also ask

What happens when you link a static library?

Static Linking and Static Libraries is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory.

What is the difference between using dynamically and statically linked libraries?

What are the differences between static and dynamic libraries? Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.

What are the advantages of static linking?

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 is the advantage of using static library?

Another benefit of using static libraries is execution speed at run-time. Because the it's object code (binary) is already included in the executable file, multiple calls to functions can be handled much more quickly than a dynamic library's code, which needs to be called from files outside of the executable.


1 Answers

You have been hit by what is popularly known as static initialization order fiasco. The basics is that the order of initialization of static objects across translation units is unspecified. See this

The call here Registry::get_registry().memberRegistered; in "shell.cpp" may happen before the call here int x = dummy(); in "member.cpp"

EDIT:

Well, x isn't ODR-used. Therefore, the compiler is permitted not to evaluate int x = dummy(); before or after entering main(), or even at all.

Just a quote about it from CppReference (emphasis mine)

It is implementation-defined whether dynamic initialization happens-before the first statement of the main function (for statics) or the initial function of the thread (for thread-locals), or deferred to happen after.

If the initialization is deferred to happen after the first statement of main/thread function, it happens before the first odr-use of any variable with static/thread storage duration defined in the same translation unit as the variable to be initialized. If no variable or function is odr-used from a given translation unit, the non-local variables defined in that translation unit may never be initialized (this models the behavior of an on-demand dynamic library)...


The only way to get your program working as you want is to make sure x is ODR-used

shell.cpp

#include <iostream>
#include "registry.h"

class shell {
public:
    shell() {};
    void init() {
        std::cout << Registry::get_registry().memberRegistered;
    };
};

extern int x;   //or extern int dummy();

int main() {
    shell *cf = new shell;
    cf->init();
    int k = x;   //or dummy();
}

^ Now, your program should work as expected. :-)

like image 174
WhiZTiM Avatar answered Sep 27 '22 16:09

WhiZTiM