为什么我不能使用.so库中声明的函数



我正试图编写一个程序来调用Android中.so库中的函数。

main.cpp:

#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
typedef int (*test_ptr)(int, int);
int main()
{
    int rc;
    void *handle;
    const char *error;
    handle = dlopen("./testdll.so", RTLD_LAZY);
    if(!handle){
        printf("dlopen %sn", dlerror());
        return -1;
        }
    printf("dlopen success.n");
    test_ptr test = (test_ptr)dlsym(handle, "max");
    error = dlerror();
    if (error)  {
        printf("error %sn", error);
        dlclose(handle);
        return -1;
    }
    rc = test(10, 20);
    printf("rc = %dn", rc);
    dlclose(handle);
    return 1;
}

testdll.cpp:

#include<stdio.h>
int max(int x,int y){
    return x>y?x:y;
}

Android.mk:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= 
    testdll.cpp
LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := true
LOCAL_LDFLAGS := -Wl,--export-dynamic
LOCAL_MODULE:= testdll
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= 
    main.cpp
LOCAL_LDLIBS := -ldl
LOCAL_SHARED_LIBRARIES := 
libcutils   
libdl
LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := true
LOCAL_MODULE := mytest
LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
include $(BUILD_EXECUTABLE)

我使用mmm制作文件并将testdll.so推入/system/lib,将mytest推向/data/data

然后我运行/mytest,它可以打开testdll。所以成功了,但无法连接我的函数。

错误为:

Symbol not found:

有人知道如何解决这个问题吗?

====================================================

谢谢大家!

我使用外部"C"调用成功的函数。

但现在,我改为使用C++编写.so库,并得到了相同的错误消息。

testdll.cpp:

#include <sys/types.h>
#include <unistd.h>
#include <grp.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>
#include <private/android_filesystem_config.h>
#include <stdio.h>
#include "testdll.h"
using namespace android;
extern "C" {
    int TestClass::max(int x,int y){
        return x>y?x:y;
    }
}

testdll.h:

using namespace android;
class TestClass
{
    public:
        int max(int, int);
};

main.h:

#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
typedef int (*test_ptr)(int, int);
int main()
{
    int rc;
    void *handle;
    const char *error;
    handle = dlopen("./testdll.so", RTLD_LAZY);
    if(!handle){
        printf("dlopen %sn", dlerror());
        return -1;
    }
    printf("dlopen success.n");
    test_ptr test = (test_ptr)dlsym(handle, "max");
    error = dlerror();
    if (error)  {
        printf("error %sn", error);
        dlclose(handle);
        return -1;
    }
    rc = test(10, 20);
    printf("rc = %dn", rc);
    dlclose(handle);
    return 1;
}

然后我得到了相同的错误消息:未找到符号:

我使用objdump-TC来显示:

DYNAMIC SYMBOL TABLE:
000003f5 g    DF .text  0000000a TestClass::max(int, int)
00000000      DF *UND*  00000000 __aeabi_unwind_cpp_pr0
00000000      DF *UND*  00000000 __cxa_finalize
000010f0 g    D  .bss   00000000 __dso_handle
00001000 g    D  .init_array    00000000 __INIT_ARRAY__
00001008 g    D  .fini_array    00000000 __FINI_ARRAY__
00000410 g    D  *ABS*  00000000 __exidx_end
00000400 g    D  *ABS*  00000000 __exidx_start
000010ec g    D  *ABS*  00000000 __data_start
000010ec g    D  *ABS*  00000000 _edata
000010ec g    D  *ABS*  00000000 __bss_start
000010ec g    D  *ABS*  00000000 __bss_start__
00001100 g    D  *ABS*  00000000 _bss_end__
00001100 g    D  *ABS*  00000000 __bss_end__
00001100 g    D  *ABS*  00000000 __end__
00001100 g    D  *ABS*  00000000 _end
00080000 g    D  *ABS*  00000000 _stack

如何修改?

再次感谢!

符号表中的TestClass::max(int,int)-这是否意味着max函数是一个类的方法?

如果是这样的话,您需要为您的TestClass创建一个C包装,例如

extern "C"
{
    int max (void *pHandle, int x, int y) 
    {
      TestClass *tcObject = (TestClass *)pHandle;
      return tcObject->max(x, y);
    }
}

计划通过dlopen访问的任何函数都必须声明为extern "C"。原因是名字被篡改了。

最新更新