Problem when calling a function when it is in .lib

I have a class with a static method that looks something like this:

class X {
    static float getFloat(MyBase& obj) {
        return obj.value();  // MyBase::value() is virtual
    }
};

I call it with an instance of MyDerived, which subclasses MyBase:

MyDerived d;
float f = X::getFloat(d);

If I link the obj file containing X to my executable, everything works as expected. If I expect to get 3.14, I get it.

If I create a .lib containing an X.obj file and a link in .lib, it breaks. When I call getFloat (), it returns -1. # IND00 . Is this some kind of sentinel value that should tell me what's wrong here?

Is there anything else when you directly link in lib and not obj?

I do not get any warnings or compiler errors.

Edit:
Visual Studio 2005 Windows XP Pro SP3. , , value() value2() . .

# 2:
, , , value(). () . , vtable . , , , - .


! ( )
, (ODR), , . - Visual ++, . /d1reportSingleClassLayout .

MyBase MyDerived , . , #ifdef, #define , (). , , , ?

, , - . .

+3
3

, lib MyDerived (.. .h/.hh/.hpp , MyDerived. . , .

, , MyDerived getFloat . vtable MyDerived ( ) vtable - MyDerived, ( .) - spring .

+1

lib , .obj , , ( ?) .

, , - MyBase, , , , . , MyBase value, , v- .

+1

. , .h, , .lib, . , .lib .

If you use visual studio instead of explicitly specifying the .lib file, simply right-click on the project and install the dependencies on the .lib project. This way you will make sure that it is using the correct .lib file.

0
source

Source: https://habr.com/ru/post/1704386/


All Articles