C ++ linker error when using O1 optimization

In an already existing and fairly large project, I enable gcc compiler optimization O1.

Without this option, everything builds, binds and works fine. with the option turned on, the main executable compilations and links, but I get linker errors when linking one of the unit tests.

Between these two compilation runs, I did not change either the code or the files, but only the optimization flag O1.

The error I get is:

Configuration.a(Builder.o): In function `Builder::Create() const':
Builder.cpp:123: undefined reference to `Factory::Instance()'

Somewhere during the binding process, he encounters this line (line 123) Builder.cpp: cpp file

pObject = Factory::Instance()->CreateObject();

(Besides the function linker error Instance, I also get one of the function CreateObject)

When I look at Factory, I see: the header file

class Factory { public:
    static Factory* Instance(); << rest of the file >>

And in the cpp file

Factory* Factory::sInstance = 0;

Factory* Factory::Instance() {
    // Check if this is the first call
    if (sInstance == 0)
    {
        // Create only instance
        sInstance = new Factory();
    }

    // Address of the instance
    return sInstance; }

, . nm Factory.o ( ) .

, , Factory::Instance(). - , , .

Google , -, .

- , ?

+5
1

Configuration.a(Builder.o): Builder::Create() const': Builder.cpp:123: undefined reference to Factory:: () '

, Factory::Instance() Factory.o, , , , , Factory.o Configuration.a, .

.

+1

All Articles