在C 中使用DLSYM加载共享对象功能

Issue loading shared object functions with dlsym in C++

本文关键字:加载 共享 对象 功能 DLSYM      更新时间:2023-10-16

我正在尝试从我安装的共享对象(.so)库中加载两个函数,但是当我编译代码时,当我尝试使用这些函数时,我会在尝试使用这些错误时会遇到一些错误功能,这是我的代码:

void G729ToPcmFilter::AudioChunkIn(AudioChunkRef& inputAudioChunk){
    std::string s = std::to_string(inputAudioChunk->GetNumBytes());
    LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn Size => " + s));
    typedef struct bcg729DecoderChannelContextStruct_struct bcg729DecoderChannelContextStruct;
    void *handle;
    bcg729DecoderChannelContextStruct (*initer)(bcg729DecoderChannelContextStruct);
    void (*decoder)(void);
    char* error;
    handle = dlopen ("libbcg729.so", RTLD_LAZY);
    if (!handle) {
            LOG4CXX_ERROR(LOG.rootLog, CStdString("Couldn't load Bcg729 plugin"));
        }
    else {
            *(void **) (&initer) = dlsym(handle,"initBcg729DecoderChannel");
            *(void **) (&decoder) = dlsym(handle,"bcg729Decoder");
            if ((error = dlerror()) != NULL)  {
                std::string str(error);
                LOG4CXX_ERROR(LOG.rootLog, CStdString("Couldn't load Bcg729 plugin's functions => "+str));
            } else {
                const char* inputBuffer = reinterpret_cast<const char*>((unsigned char*)inputAudioChunk->m_pBuffer);
                char *ret = (char*)malloc(10);
                memcpy(ret, inputBuffer, 10);
                char* firstFragment = ret;
                ret = (char*)malloc(10);
                memcpy(ret, inputBuffer+10, 10);
                char* secondFragment = ret;
                LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn buffer separated into two 10 bit buffers"));
                int16_t outputBuffer[80]; /* output buffer: the reconstructed signal */ 
                int16_t outputBuffer1[80]; /* output buffer: the reconstructed signal */ 
                uint8_t bitStream[10]; /* binary input for the decoder */
                LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn Buffers Created"));
                bcg729DecoderChannelContextStruct *decoderChannelContext = (*initer)();
                LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn Decoder Initialized"));
                for(int i=0; i < 10 ; i++){
                    bitStream[i] = (uint8_t)atoi(&firstFragment[i]);
                }
                LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn First BitStream Created"));
                (*decoder)(decoderChannelContext, bitStream, 1, outputBuffer);
                LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn First Stream Decoding DONE"));
                for(int i=0; i < 10 ; i++){
                    bitStream[i] = (uint8_t)atoi(&secondFragment[i]);
                }
                LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn Second BitStream Created"));
                (*decoder)(decoderChannelContext, bitStream, 0, outputBuffer1);
                LOG4CXX_ERROR(LOG.rootLog, CStdString("G729 AudioChunkIn Second Stream Decoding DONE"));
                m_outputAudioChunk.reset(new AudioChunk());
                AudioChunkDetails chunkDetails = *inputAudioChunk->GetDetails();
                chunkDetails.m_rtpPayloadType = -1;
                chunkDetails.m_encoding = PcmAudio;
                chunkDetails.m_numBytes = 160;
                short* outputBufferFinal = (short*)m_outputAudioChunk->CreateBuffer(chunkDetails);
                for(int i = 0; i < 80; i++){
                    outputBufferFinal[i] = outputBuffer[i];
                    outputBufferFinal[i+80] = outputBuffer1[1];
                }
            }
    }
}

我得到的,在编译时会出现以下错误,我敢肯定,我正在使用的.so库上的参数:

G729Codec.cpp: In member function ‘virtual void G729ToPcmFilter::AudioChunkIn(AudioChunkRef&)’:
G729Codec.cpp:173:74: error: too few arguments to function
     bcg729DecoderChannelContextStruct *decoderChannelContext = (*initer)();
                                                                          ^
G729Codec.cpp:183:65: error: too many arguments to function
     (*decoder)(decoderChannelContext, bitStream, 1, outputBuffer);
                                                                 ^
G729Codec.cpp:193:66: error: too many arguments to function
     (*decoder)(decoderChannelContext, bitStream, 0, outputBuffer1);

在libbcg729.so库的标题上,该函数的声明如下:

BCG729_VISIBILITY bcg729DecoderChannelContextStruct *initBcg729DecoderChannel();
BCG729_VISIBILITY void bcg729Decoder(bcg729DecoderChannelContextStruct *decoderChannelContext, uint8_t bitStream[], uint8_t frameErasureFlag, int16_t signal[]);

我无法弄清楚我做错了什么,有人可以帮我吗?

看起来像您对函数指针的声明:

BCG729DECODERCODERCODERCONNELCONTEXTSTRUCT(*Inter)(BCG729DECODERCODERCODERCHANNELCONTEXTSTRUCT);
void(*解码器)(void);

是错误的,我想它们应该是:

bcg729decoderCoderConnelContextStruct*(* inter)()();
void BCG729DECODER(...有关BCG729DECODER()函数签名,请参见BCG729库标头文件

您可能还需要检查是否需要将这些声明指定为外部" C",因为BCG729库是用C而不是C 编写的。