How to prevent the linker from optimizing away startup code?

后端 未结 2 1770
北荒
北荒 2021-01-14 12:19

I have the following problem: My (C++-)project consists of several subprojects. In each, I have several files with code I want to run at startup. My solution so far is to us

相关标签:
2条回答
  • 2021-01-14 12:45

    There are no standard conformant ways of forcing objects in libraries to be initialised - you have to use tricks depending on your particular platform(s). The difference between a DLL and and a static library (on Windows, at least) is that the former has start-up and shut-down code that is executed by the OS, whereas the latter is just a concatenation of object files.

    Also, the linker is not optimising away your start up code - it is simply not linking it, because it apparently is never used. Linkers are pretty stupid beasts - if you want to find out how they do what they do, take a look at the book Linkers & Loaders.

    0 讨论(0)
  • 2021-01-14 12:59

    Some trick, but review it. For Win system (but not linux) use explicit dllexport - in this case linker doen't knows if this symbol used by outer app or not.

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