Linux下加載庫的問題(dlopenm, dlsym)
如題,?程序中發現load庫成功,但是加載函數的時候報錯:?undefined?symbol?functionname
是很簡單的一個東西,因為不熟悉,所以老是弄不好,請各位指導!
代碼如下:
//?SCTLX_DefaultOperationManager.h文件
#ifndef__SCTLX_DEFAULTOPERATIONMANAGER_H
#define__SCTLX_DEFAULTOPERATIONMANAGER_H
/******************************************
名稱:SCTDefaultOperationManager
描述:默認操作功能
參數:
pszLeft:接口預留
nLeft:接口預留
pszErrInfo:錯誤信息
返回:
0:成功
-1:失敗
******************************************/
extern?"C"?int?SCTDefaultOperation(char*?pszLeft,?int?nLeft,?char*?pszErrInfo);
#endif//__SCTLX_DEFAULTOPERATIONMANAGER_H
----------------------------------------------------------------------------------
//?cpp文件
#include?"stdlib.h"
#include?"stdio.h"
#include?"string.h"
using?namespace?std;
int?SCTDefaultOperation(char*?pszLeft,?int?nLeft,?char*?pszErrInfo)
{
charpLeft[1024];
intiLeft;
charpErrInfo[1024];
memset(pLeft,?0x00,?1024);
memset(pErrInfo,?0x00,?1024);
iLeft?=?0;
memcpy(pLeft,?pszLeft,?1024);
memcpy(pErrInfo,?pszErrInfo,?1024);
iLeft?=?nLeft;
printf("默認操作:?什么也不做!...\n");
return?0;
}
----------------------------------------------------------
/
//?主程序
...
typedefint(*Function)(char*?pszLeft,?int?nLeft,?char*?pszErrInfo);
stringg_strDllList[1024];
Functiong_FuncList[1024];
void*g_dl[1024];
...
intiLoadDll(const?char*?pDllPath)
{
intiDllNo;
iDllNo?=?-1;
if?(pDllPath?==?NULL)
return?-1;
if?(strlen(pDllPath)?==?0)
return?-1;
for?(iDllNo=0;?iDllNo<1024;?iDllNo++)
{
if?(g_strDllList[iDllNo].length()?!=?0?&&?g_strDllList[iDllNo]?==?pDllPath)
{
printf("lib?has?been?loaded,?skipping...\n",?pDllPath);
return?iDllNo;
}
}
for?(iDllNo=0;?iDllNo<1024;?iDllNo++)
{
if?(g_dl[iDllNo]?==?NULL)
{
g_dl[iDllNo]?=?dlopen(pDllPath,?RTLD_NOW);
if?(dlerror()?!=?NULL)
return?-1;
else
{
printf("load?lib?successed:?%s\n",?pDllPath);
g_strDllList[iDllNo]?=?pDllPath;
return?iDllNo;
}
}
}
}
intiLoadFunc(int?iNodeNo,?int?iDllNo,?const?char*?pFuncName)
{
if?(iNodeNo??1024)
return?-1;
if?(iDllNo??1024)
return?-1;
if?(pFuncName?==?NULL)
return?-1;
if?(strlen(pFuncName)?==?0)
return?-1;
printf("NodeNo:?%d,?load?function:?%s\n",?iNodeNo,?pFuncName);
g_FuncList[iNodeNo]?=?(Function)dlsym(g_dl[iDllNo],?pFuncName);
if?(g_FuncList[iNodeNo]?==?NULL)
{
printf("%s\n",?dlerror());
printf("NodeNo:?%d,?load?function?failed!\n",?iNodeNo);
return?-1;
}
return?0;
}
------------------------------------------------------------
/
//makefile
.PHONY:?default
default:
g++?-Wall?-W?-shared?-fPIC?-o?$(TargetPath)libSCTLX_DefaultOperationManager.so?$(DefaultPath)SCTLX_DefaultOperationManager.cpp
.PHONY:?dpscheck
dpscheck:
g++?-ldl?-o?$(TargetPath)dpscheck?SCTLX_Maintain.cpp?libini.a
網上搜了一些,大多是說要加個extern?"C"的。。。。
分享到:
更多
------解決方案--------------------
你可以去看看gcc和g++的區別