在C 中使用DLSYM加载共享对象功能
Issue loading shared object functions with dlsym in C++
我正在尝试从我安装的共享对象(.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 编写的。
相关文章:
- 加载共享库时C++错误:libopencv_ximgproc.so.4.4
- 从 CMake 中的库目录加载共享库?
- 无法使用 python ctypes 加载C++共享库
- ./main:加载共享库时出错:libopencv_highgui.so.4.0:无法打开共享对象文件:没有这样的文件或
- 在 win32 上生成 R 包:无法加载共享对象 (.dll)
- 加载共享库时出错:libbsoncxx.so._noabi:无法打开共享对象文件:没有此类文件或目录
- 在 Linux 上,在 C++ 程序中,如何找到已加载共享库的路径?
- 如何更改路径以修复错误"./main:加载共享库 libmkl_core.so 时出错?
- 正在加载共享对象:文件中未定义版本Qt_5
- 将生成文件转换为CMakeLists;无法加载共享库
- 加载共享库时出现"错误:libSDL2_mixer-2.0.so.0:无法打开共享对象文件:没有这样的文件或目录
- 使用dlopen动态加载共享库
- 交叉编译 qt:加载共享库时出错
- 加载共享库时出现 Codelite 错误,但我可以编译它
- 加载共享库时出错:JVM.dll
- 如何在Linux上热重新加载共享库
- 在共享C++库中加载共享库时C++未定义的符号,该库本身由 Python 加载
- 在 Windows 中加载共享库时在特定路径中选取 dll
- 一种在没有 root 的情况下加载共享库的更简单方法
- C++ Linux 加载共享库时出错“未定义的符号:pthread_create”