How to force gcc to link unreferenced, static C++ objects from a library

微笑、不失礼 提交于 2019-11-27 20:06:09

You can use -Wl,--whole-archive -lyourlib , see the manpage for ld for more info.

Any static libraries mentioned after -Wl,--whole-archive on the command line gets fully included, you can turn this off again too if you need to , as in e.g. -Wl,--whole-archive -lyourlib -Wl,--no-whole-archive -lotherlib

A kind of hack helped me out. Still have to include the header although.

// in the header with your class
struct RegistrationHelper {
    RegistrationHelper();
};

static RegistrationHelper Helper;

// in your cpp where you register your class
RegistrationHelper::RegistrationHelper() {}

Use:

g++ -u <SYMBOL_NAME> ...

Note that -u is lowercase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!