本文介绍了GCC错误:无法offsetof适用于成员函数MyClass的:: MyFunction的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图取代偏移与关键字之后的 __ offsetof 在试图与苹果公司编制的 GCC 4.2.1 使用 -fasm -blocks 参数(这使得英特尔的风格汇编语法),内联汇编code这在MSVC的工作,我得到一个错误:无法适用offsetof,来成员函数MyClass的:: MyFunction的

After trying to replace the offset keyword with __offsetof while trying to compile with Apple GCC 4.2.1 using the -fasm-blocks argument (which enables Intel style assembly syntax) inline assembly code which worked in MSVC, I get an error: Cannot apply offsetof to member function MyClass::MyFunction

class MyClass
{
    void MyFunction(void* pData)
    {

    }
};

void test()
{
    _asm
    {
        //mov eax, offset MyClass::MyFunction - this works in MSVC
        mov eax, offsetof(class MyClass, MyFunction) //error: Cannot apply offsetof to member function MyClass::MyFunction
        mov eax, __offsetof(class MyClass, MyFunction) //error: Invalid cast from type 'void (MyClass::*)(void*)' to type size_t
    };
}


有人可以告诉我,我该怎么办?看来,我移植应用程序的整个结构是基于这个该死的偏移宏...

Can somebody please tell me, what should I do? It seems that the whole structure of the application I'm porting is based on this damn offset macro...

推荐答案

offsetof 得到的偏移,从结构开始一员,但功能的的成员在这个意义上(甚至不是虚函数)。你可能以后是什么偏移关键字:

offsetof gets the offset of a member from the start of a structure, but functions are not members in that sense (not even virtual functions). what you are probably after is the offset keyword:

class A
{
    int Func1()
    {
        return 1;
    }
};

__declspec(naked) int GetAddress()
{
    __asm
    {
        mov eax, offset A::Func1
        retn
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    printf("0x%08X\n",GetAddress());
    return 0;
}

要获得下GCC相同的效果(我使用-masm =英特尔,作为-fasm块只有苹果),我们会做(32位exe文件,Windows 7的64位下进行测试,使用GCC 4.4):

To get the same effect under GCC (I'm using -masm=intel, as -fasm-blocks is apple only) we would do (32bit exe, tested under windows 7 64bit, using GCC 4.4):

class A
{
public:
    int Func1() __attribute__((noinline))
    {
        return 1;
    }
};

int a = 0; //this is bad, but it works for this demo
int GetAddress()// __attribute__((noinline))
{
    __asm("mov dword ptr[_a], offset __ZN1A5Func1Ev");
    return a;
}

int main()
{
    A a;
    printf("%d\n",a.Func1());
    system("pause");
    printf("0x%08X\n",GetAddress());
    return 0;
}

使用GCC的问题是:

The problem with GCC is that:


  1. 您不能绑定自动内联函数,不产生符号(因此noinline始终)。

  2. 您需要绑定的装饰符号,除非你强制未修饰的符号(即使如此,仍然会有一些装修)

这篇关于GCC错误:无法offsetof适用于成员函数MyClass的:: MyFunction的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:50