How do I make an unreferenced object load in C++?

前端 未结 8 1848
一向
一向 2021-01-03 16:36

I have a .cpp file (let\'s call it statinit.cpp) compiled and linked into my executable using gcc. My main() function is

相关标签:
8条回答
  • 2021-01-03 16:50

    Of course the dynamic library solution is the best, but I've also been told it's possible to link the whole static library with the linker option:

    -Wl,-whole-archive
    

    before the library's -l option, and

    -Wl,-no-whole-archive
    

    after it (to avoid including other libraries as a whole, too).

    0 讨论(0)
  • 2021-01-03 16:51

    I ran into the same problem.

    Write a file, DoNotOptimizeAway.cpp:

    void NoDeadcodeElimination()
    {
        // Here use at least once each of the variables that you'll need.
    }
    

    Then call NoDeadcodeElimination() from main.

    EDIT: alternatively you can edit your linker options and tell it to always link everything, even if it's not used. I don't like this approach though since executables will get much bigger.

    0 讨论(0)
  • 2021-01-03 17:00

    Read the manual page for the ld command and look at the -u option. If statinit.cpp defines anything that looks like a function then try naming it in -u. Otherwise choose a data object that's defined in statinit.cpp and name that in -u, and hope it works. I think it's best if you write the command line so the -u option comes immediately before your -l option for your library that has statinit's object code in it.

    0 讨论(0)
  • 2021-01-03 17:02

    It is not exactly clear what the problem is:

    C++ does not have the concept of static initializers.
    So one presume you have an object in "File Scope".

    • If this object is in the global namespace then it will be constructed before main() is called and destroyed after main() exits (assuming it is in the application).
    • If this object is in a namespace then optionally the implementation can opt to lazy initialize the variable. This just means that it will be fully initialized before first use. So if you are relying on a side affect from construction then put the object in the global namespace.

    Now a reason you may not be seeing the constructor to this object execute is that it was not linked into the application. This is a linker issue and not a language issue. This happens when the object is compiled into a static library and your application is then linked against the static library. The linker will only load into the application functions/objects that are explicitly referenced from the application (ie things that resolve undefined things in the symbol table).

    To solve this problem you have a couple of options.

    • Don't use static libraries.
      • Compile into dynamic libraries (the norm nowadays).
      • Compile all the source directly into the application.
    • Make an explicit reference to the object from within main.
    0 讨论(0)
  • 2021-01-03 17:08

    Is the problem that the static items were never initialized, or is the problem that the static items weren't initialized when you needed to use them?

    All static initialization is supposed to be finished before your main() runs. However, you can run into issues if you initialize on static object with another static object. (Note: this doesn't apply if you are using primitives such as int)

    For example, if you have in file x.cpp:

    static myClass x(someVals);
    

    And in y.cpp:

    static myClass y = x * 2; 
    

    It's possible that the system will try to instantiate y before x is created. In that case, the "y" variable will likely be 0, as x is likely 0 before it is initialized.

    In general, the best solution for this is to instantiate the object when it is first used (if possible). However, I noticed above you weren't allowed to modify that file. Are the values from that file being used elsewhere, and maybe you can change how those values are accessed?

    0 讨论(0)
  • 2021-01-03 17:10

    These problems, and the problems with these potential solutions all revolve around the fact that you can't guarantee much about static initialization. So since it's not dependable, don't depend on it!

    Explicitly initialize data with a static "InititalizeLibrary" type static function. Now you guarantee it happens, and you guarantee when it happens in relation to other code based on when you make the call.

    0 讨论(0)
提交回复
热议问题