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

前端 未结 8 1952
一向
一向 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: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.

提交回复
热议问题