0

I have two source files, in one of them there's a static member from my own class, foo. When I compile it to an object file, the compiler generates a .ctors section, and within it, a function that calls the constructor, lets call it _GLOBAL__SUB_I_FOO.

In the second source file, I try to call this constructor myself:

int _GLOBAL__SUB_I_FOO();
int bar(){
   _GLOBAL__SUB_I_FOO();
}

And when I'm linking those two objects, there's an undefined reference to _GLOBAL__SUB_I_FOO from the second file. When I'm linking using --relocatable to view what went wrong, I can see that the original constructor was renamed to _GLOBAL__SUB_I_FOO_0 to avoid the "collision" (which is intentional). What can I do to fix it? I'm using gcc 7.3.0

ByoTic
  • 333
  • 1
  • 4
  • 16

1 Answers1

0

To execute the code that was previously in the constructor for the static object, move it into an init() function, and call that from the constructor (and from anywhere else you need it).

Tim Randall
  • 4,040
  • 1
  • 17
  • 39
  • It is possible, but not the answer I am looking for, I'm looking for more hermetic solution that will let me call any ctor without having to implement it inside another function. But thanks, though. – ByoTic Aug 07 '18 at 17:27
  • The problem with static objects is that they are constructed, once, at a time determined by the implementation. It's one object and as such it can only be constructed once. Perhaps you'd get better mileage from something based on the `Singleton` pattern – Tim Randall Aug 07 '18 at 17:46
  • If you want to know, I'm actually implementing the mechanism that invokes the constructors. Just.. go with me on this one, I know it isn't standard but there got to be a way to do it. – ByoTic Aug 07 '18 at 17:51
  • I don't think gcc 7.3.0 is compatible with "go with me on this". It's always going to pick a unique name for its internal function, and it's always going to do so _after_ any changes _you_ make to the code. And if by chance you do find a hack, maybe by putting in an adjacent object and jumping relative to its address, the hack is still prone to failure on any other compiler, or other version of that compiler. I just think that what you're trying to do... is wrong. – Tim Randall Aug 07 '18 at 18:04
  • Are you saying this because you know, or that's your feeling? Because it's weird to me that the ld knows that this is an internal function - and if it knows, there must be a way to mark it as a "normal" function. I'm not looking for a hack, its just calling a function from a different object. – ByoTic Aug 07 '18 at 18:12
  • The initial underscore tells us it's an internal function. But so does the C++ standard. Construction of globals is determined by the implementation. I think perhaps the problem is that you're focussing on this compiler-generated function, and not on the constructor. The latter is a function written by you, and calling it isn't at all difficult. You can use placement new, yes? – Tim Randall Aug 07 '18 at 18:30
  • I prefer to just iterate through .ctors and calls what's in there, because there is no list of static objects on which I can call placement new on. – ByoTic Aug 07 '18 at 18:43
  • If you want to run _all_ the constructors again, simply quit and re-run the program – Tim Randall Aug 07 '18 at 18:45