I have a global in a static library which registers and deregisters from a registrar in its ctor/dtor, however without passing -u symbol
or --whole-archive
it never gets called.
With msvc you can force inclusion of a symbol using #pragma comment(linker, "/include:__mySymbol")
in code to fix this. Is there anything I can do, in code, in gcc and/or clang to do the same thing?
It's very easy to reproduce this:
Executable:
// main.cpp
int main() {}
Static Library depended on by executable:
// test.cpp
#include <iostream>
struct Test {
Test() { std::cout << "Test()\n"; }
~Test() { std::cout << "~Test()\n"; }
};
Test test;
If the static library is a shared library, the program prints. If the static library uses --whole-archive
, the program prints. But I want to control this in code, not with suboptimal compile flags (or for -u symbol
: compile flags dependant on symbol names in code).