3

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).

David
  • 27,652
  • 18
  • 89
  • 138
  • 2
    Can't you just reference the global variable? For example declaring a global `void *include_test = &global_test;`. – rodrigo Mar 29 '18 at 17:29
  • @rodrigo Doesn't work in test.cpp. Where would I put that? – David Mar 29 '18 at 17:36
  • 1
    The reference to the global should be in the main executable, of course. – rodrigo Mar 29 '18 at 17:46
  • 1
    @rodrigo: That's a bit messy though, requiring every calling executable to perform hacks on behalf of the library code. – Lightness Races in Orbit Mar 29 '18 at 18:11
  • 1
    @LightnessRacesinOrbit: Well, the OP does not want to use a linker option, he wants a source code solution instead. If there are many executables, you'll have to repeat the code. You could use a header file to avoid repeating yourself... – rodrigo Mar 29 '18 at 18:41
  • Does it have to be a static library? This is not an issue if you use a dynamic library. – Martin York Mar 29 '18 at 18:43
  • Does this help: https://stackoverflow.com/q/18234945/14065 You just need to touch the global variable from the init function (just get its address). – Martin York Mar 29 '18 at 18:46
  • 1
    FWIW I "solved" this using `--whole-archive` and never looked back. (If there's a better way I never found it, though my lib is tiny so didn't try very hard) – Lightness Races in Orbit Mar 29 '18 at 18:48
  • @rodrigo it's a big system, this is a generic library used by many of executables. Having the reference in the executable defeats the purpose to me. – David Mar 29 '18 at 19:15
  • @MartinYork I mentioned that in the question too, but I would like it to be a static lib. I'll use `--whole-archive` before I make it a dynamic lib. Or I'll pull out _just_ the registration into a seperate static lib with `--whole_archive` so it doesn't bloat too much. – David Mar 29 '18 at 19:16
  • @rodrigo The OP wants a *local* in-source solution. Ie, in `test.cpp` being able to say "btw, consider this module used, even if it isn't". Having to mention this elsewhere (in a linking list or in main or whatever) is crappy. – Yakk - Adam Nevraumont Jun 05 '23 at 15:11

0 Answers0