I get an undefined reference to __cxa_guard_acquire linker error when I use function scoped static classes in C++.
If the function can only be called by one thread of execution then set the compiler additional option -fno-threadsafe-statics. If the function can be called by multiple threads of execution then you will need to implement a resource lock/unlock when __cxa_guard_acquire/__cxa_guard-release are called.
Comments
4 comments
Hello, To resolve this undefined references (__cxa_guard_acquire ) , add a library flag "-lstdc++" in your Makefile. It should work. It worked for me.
T&R,
Pintu
I'm trying to implement the Singleton pattern and am getting this same error. Can anyone please elaborate on the steps required to solve this issue? Thanks.
I get the same thing on a singleton pattern. Why?
static Graphics_c& GetInstance(){static Graphics_c instance; return instance;};
This implementation of a singleton pattern will cause the issue (and can be fixed as Paul describes above):
rb_controller& rb_controller::singleton()
{
static rb_controller singleton_instance;
return singleton_instance;
}
This implementation doesn't cause the issue in the first place:
class rb_controller {
private:
static rb_controller *singleton_instance;
public:
};
rb_controller *rb_controller::singleton_instance = NULL;
rb_controller& rb_controller::singleton()
{
if (singleton_instance == NULL) {
singleton_instance = new rb_controller();
}
return *singleton_instance;
}
}
Please sign in to leave a comment.