Wrong arguments position in function imported with

2019-07-24 19:32发布

I have strange issue. When I invoking imported method with arguments from shared library, in those method I have wrong arguments. It's like:

x = 1; y = 2; z = 3;
(*method)(x,y,z);

In method I have:

void method(int x, int y, int z){
    // x = 2, y = 3, z = 32432423 - something like this
}

Here how I do import:

QVector<int> (*interpolateValue)( int, int, int );
libHandle = dlopen( "plugins/libinterpolate.so", RTLD_LAZY );
*(void **)(&interpolateValue) = dlsym( libHandle, "_ZN11Interpolate16interpolateValueEiii" );

QVector<int> ys = (*interpolateValue)( lastY, newY, step );

I made work around about this in such way:

QVector<int> (*interpolateValue)( int*, int, int, int );
QVector<int> ys = (*interpolateValue)( NULL, lastY, newY, step );

But i think it's not a means.

标签: c++ qt dlsym
1条回答
成全新的幸福
2楼-- · 2019-07-24 20:11

c++filt(1) says:

$ c++filt _ZN11Interpolate16interpolateValueEiii
Interpolate::interpolateValue(int, int, int)

which seems to indicate that the function you're trying to call is a member function of a C++ class. That means it has an implicit first parameter - the this pointer. Your workaround fixes things because you're passing a NULL this pointer for the method to use. Apparently it does not, in fact, use that parameter at all, though..

If you don't want to have to work around the problem in this way, change Interpolate::interpolateValue(int, int, int) to be a free function instead of a method of whatever class it's in.

查看更多
登录 后发表回答