日期:2014-05-16  浏览次数:20733 次

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)
{
char pLeft[1024];
int iLeft;
char pErrInfo[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;
}
----------------------------------------------------------
/////////////////////////////////////////////////
// 主程序
...
typedef int (*Function)(char* pszLeft, int nLeft, char* pszErrInfo);
string g_strDllList[1024];
Function g_FuncList[1024];
void* g_dl[1024];
...

int iLoadDll(const char* pDllPath)
{
int iDllNo;

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;
}
}
}
}

int iLoadFunc(int iNodeNo, int iDllNo, const char* pFuncName)
{
if (iNodeNo < 0 || iNodeNo > 1024)
return -1;

if (iDllNo < 0 || 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