linkage fails when referencing a constructor

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



linkage fails when referencing a constructor



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





Why not just call the constructor (create an object)?
– NathanOliver
Aug 7 at 16:26





It's a constructor of a static object
– ByoTic
Aug 7 at 16:42





Did the compiler create this function _GLOBAL__SIB_I_FOO or is that a function you wrote?
– SirGuy
Aug 7 at 16:44


_GLOBAL__SIB_I_FOO





Why are you trying to call this function anyway? It seems to me like it's supposed to stay hidden as an implementation detail.
– SirGuy
Aug 7 at 16:49





Given that the name of the function is one reserved for the implementation, it is fairly clear that you are not supposed to call it yourself (even if you technically can).
– Jesper Juhl
Aug 7 at 18:17





1 Answer
1



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


init()





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 7 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 7 at 17:46



Singleton





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 7 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 7 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 7 at 18:12






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered