现有一个第三方so文件(比如:libXXX.so),有详细的接口文档,可以在android下直接调用,但现需要用C再封装一层,自己写C调用第三方的libXXX.so,再编译成自已的so文件,android中再调用自己的so文件。
问题:
1.C下如何调用libXXX.so?
2.Android.mk文件如何编写?
请贴详细代码,多谢!
13 个解决方案
#1
1.我记得应该是dlopen吧
2.LOCAL_SHARED_LIBRARIES 后加引用的动态库
仅供参考....
2.LOCAL_SHARED_LIBRARIES 后加引用的动态库
仅供参考....
#2
我看到hal层里面都是通过dlopen去打开动态库的
#3
LOCAL_PATH := $(call my-dir)
## XXX
#
include $(CLEAR_VARS)
LOCAL_MODULE := XXX
LOCAL_SRC_FILES := XXX.c
LOCAL_CFLAGS := -Iyour_lib_name/include
LOCAL_LDFLAGS += -Lyour_lib_name/lib
LOCAL_LDLIBS := -lyour_lib_name
include $(BUILD_SHARED_LIBRARY)
## XXX
#
include $(CLEAR_VARS)
LOCAL_MODULE := XXX
LOCAL_SRC_FILES := XXX.c
LOCAL_CFLAGS := -Iyour_lib_name/include
LOCAL_LDFLAGS += -Lyour_lib_name/lib
LOCAL_LDLIBS := -lyour_lib_name
include $(BUILD_SHARED_LIBRARY)
#4
有详细代码吗?最好 Android.mk跟C调用的都有。
#5
所有Android.mk的代码都在上面了,C代码跟安卓不安卓的没关系,就是基本C编程而已
#6
这个还是很对的。。。呵呵
#7
那个so是你自己写的嘛 不知道api没法调吧
#8
用C调用第三方so
void *handle;
int (*call_method)(void);
handle = dlopen(so_path, RTLD_LAZY);
//函数指针
call_method = dlsym(handle, "method_name(调用方法的函数名)");
#9
LOCAL_PATH := $(call my-dir)
## XXX
#
include $(CLEAR_VARS)
LOCAL_MODULE := XXX
LOCAL_SRC_FILES := XXX.c
LOCAL_CFLAGS := -Iyour_lib_name/include
LOCAL_LDFLAGS += -Lyour_lib_name/lib
LOCAL_LDLIBS := -lyour_lib_name
include $(BUILD_SHARED_LIBRARY) 1.我记得应该是dlopen吧
2.LOCAL_SHARED_LIBRARIES 后加引用的动态库
仅供参考....
有详细代码吗?最好 Android.mk跟C调用的都有。
用C调用第三方sovoid *handle;
int (*call_method)(void);
handle = dlopen(so_path, RTLD_LAZY);
//函数指针
call_method = dlsym(handle, "method_name(调用方法的函数名)");
多谢!我试试。
#10
C可以调用C++的.so?
#11
C调用so文件,不会
但是Android.mk文件会
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := gpio ---》自己定义要的so名称
LOCAL_SRC_FILES := gpio.c ---》你的C文件名称
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
但是Android.mk文件会
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := gpio ---》自己定义要的so名称
LOCAL_SRC_FILES := gpio.c ---》你的C文件名称
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
#12
有这麻烦?有详细的接口文档,可以在android下直接调用
应该有提供lib的头文件吧,直接在android.mk里面将头文件和lib 加入进来
然后在你自己的代码中#include头文件,直接调用相应的接口就可以了。
如果没有头文件就得使用dlopen dlsym
不过.so文件你可以直接copy到/libs/armeabi/下面
然后代码中使用绝对路径dlopen ("/data/data/lib/libxxx.so )
应该有提供lib的头文件吧,直接在android.mk里面将头文件和lib 加入进来
然后在你自己的代码中#include头文件,直接调用相应的接口就可以了。
如果没有头文件就得使用dlopen dlsym
不过.so文件你可以直接copy到/libs/armeabi/下面
然后代码中使用绝对路径dlopen ("/data/data/lib/libxxx.so )
#13
参照实例:
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE :=hello-world
LOCAL_SRC_FILES :=hello-world.c
LOCAL_SHARED_LIBRARIES := hello-jni
LOCAL_LDLIBS += -llog
include $(BUILD_SHARED_LIBRARY)
相对应的c代码:
#include "string.h"
#include "jni.h"
#include "dlfcn.h"
#include <fcntl.h>
void * filehandle = NULL;
jint (*getResult)(JNIEnv *, jobject, jint, jint) =NULL;
jint JNICALL Java_com_example_hellojni_HelloAct_getInt(JNIEnv * env,
jobject thiz) {
jint result = 0;
filehandle = dlopen("libhello-jni.so", RTLD_LAZY);
if (filehandle) {
getResult = (jint (*)(JNIEnv *, jobject, jint, jint)) dlsym(filehandle,
"Java_com_example_hellojni_HelloJni_add");
if (getResult)
result = getResult(env, thiz, 3, 10);
dlclose(filehandle);
filehandle = NULL;
}
return result + 1;
}
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE :=hello-world
LOCAL_SRC_FILES :=hello-world.c
LOCAL_SHARED_LIBRARIES := hello-jni
LOCAL_LDLIBS += -llog
include $(BUILD_SHARED_LIBRARY)
相对应的c代码:
#include "string.h"
#include "jni.h"
#include "dlfcn.h"
#include <fcntl.h>
void * filehandle = NULL;
jint (*getResult)(JNIEnv *, jobject, jint, jint) =NULL;
jint JNICALL Java_com_example_hellojni_HelloAct_getInt(JNIEnv * env,
jobject thiz) {
jint result = 0;
filehandle = dlopen("libhello-jni.so", RTLD_LAZY);
if (filehandle) {
getResult = (jint (*)(JNIEnv *, jobject, jint, jint)) dlsym(filehandle,
"Java_com_example_hellojni_HelloJni_add");
if (getResult)
result = getResult(env, thiz, 3, 10);
dlclose(filehandle);
filehandle = NULL;
}
return result + 1;
}
#1
1.我记得应该是dlopen吧
2.LOCAL_SHARED_LIBRARIES 后加引用的动态库
仅供参考....
2.LOCAL_SHARED_LIBRARIES 后加引用的动态库
仅供参考....
#2
我看到hal层里面都是通过dlopen去打开动态库的
#3
LOCAL_PATH := $(call my-dir)
## XXX
#
include $(CLEAR_VARS)
LOCAL_MODULE := XXX
LOCAL_SRC_FILES := XXX.c
LOCAL_CFLAGS := -Iyour_lib_name/include
LOCAL_LDFLAGS += -Lyour_lib_name/lib
LOCAL_LDLIBS := -lyour_lib_name
include $(BUILD_SHARED_LIBRARY)
## XXX
#
include $(CLEAR_VARS)
LOCAL_MODULE := XXX
LOCAL_SRC_FILES := XXX.c
LOCAL_CFLAGS := -Iyour_lib_name/include
LOCAL_LDFLAGS += -Lyour_lib_name/lib
LOCAL_LDLIBS := -lyour_lib_name
include $(BUILD_SHARED_LIBRARY)
#4
LOCAL_PATH := $(call my-dir)
## XXX
#
include $(CLEAR_VARS)
LOCAL_MODULE := XXX
LOCAL_SRC_FILES := XXX.c
LOCAL_CFLAGS := -Iyour_lib_name/include
LOCAL_LDFLAGS += -Lyour_lib_name/lib
LOCAL_LDLIBS := -lyour_lib_name
include $(BUILD_SHARED_LIBRARY)
1.我记得应该是dlopen吧
2.LOCAL_SHARED_LIBRARIES 后加引用的动态库
仅供参考....
有详细代码吗?最好 Android.mk跟C调用的都有。
#5
所有Android.mk的代码都在上面了,C代码跟安卓不安卓的没关系,就是基本C编程而已
#6
LOCAL_PATH := $(call my-dir)
## XXX
#
include $(CLEAR_VARS)
LOCAL_MODULE := XXX
LOCAL_SRC_FILES := XXX.c
LOCAL_CFLAGS := -Iyour_lib_name/include
LOCAL_LDFLAGS += -Lyour_lib_name/lib
LOCAL_LDLIBS := -lyour_lib_name
include $(BUILD_SHARED_LIBRARY)
#7
那个so是你自己写的嘛 不知道api没法调吧
#8
LOCAL_PATH := $(call my-dir)
## XXX
#
include $(CLEAR_VARS)
LOCAL_MODULE := XXX
LOCAL_SRC_FILES := XXX.c
LOCAL_CFLAGS := -Iyour_lib_name/include
LOCAL_LDFLAGS += -Lyour_lib_name/lib
LOCAL_LDLIBS := -lyour_lib_name
include $(BUILD_SHARED_LIBRARY) 1.我记得应该是dlopen吧
2.LOCAL_SHARED_LIBRARIES 后加引用的动态库
仅供参考....
有详细代码吗?最好 Android.mk跟C调用的都有。
用C调用第三方so
void *handle;
int (*call_method)(void);
handle = dlopen(so_path, RTLD_LAZY);
//函数指针
call_method = dlsym(handle, "method_name(调用方法的函数名)");
#9
LOCAL_PATH := $(call my-dir)
## XXX
#
include $(CLEAR_VARS)
LOCAL_MODULE := XXX
LOCAL_SRC_FILES := XXX.c
LOCAL_CFLAGS := -Iyour_lib_name/include
LOCAL_LDFLAGS += -Lyour_lib_name/lib
LOCAL_LDLIBS := -lyour_lib_name
include $(BUILD_SHARED_LIBRARY) 1.我记得应该是dlopen吧
2.LOCAL_SHARED_LIBRARIES 后加引用的动态库
仅供参考....
有详细代码吗?最好 Android.mk跟C调用的都有。
用C调用第三方sovoid *handle;
int (*call_method)(void);
handle = dlopen(so_path, RTLD_LAZY);
//函数指针
call_method = dlsym(handle, "method_name(调用方法的函数名)");
多谢!我试试。
#10
C可以调用C++的.so?
#11
C调用so文件,不会
但是Android.mk文件会
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := gpio ---》自己定义要的so名称
LOCAL_SRC_FILES := gpio.c ---》你的C文件名称
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
但是Android.mk文件会
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := gpio ---》自己定义要的so名称
LOCAL_SRC_FILES := gpio.c ---》你的C文件名称
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
#12
有这麻烦?有详细的接口文档,可以在android下直接调用
应该有提供lib的头文件吧,直接在android.mk里面将头文件和lib 加入进来
然后在你自己的代码中#include头文件,直接调用相应的接口就可以了。
如果没有头文件就得使用dlopen dlsym
不过.so文件你可以直接copy到/libs/armeabi/下面
然后代码中使用绝对路径dlopen ("/data/data/lib/libxxx.so )
应该有提供lib的头文件吧,直接在android.mk里面将头文件和lib 加入进来
然后在你自己的代码中#include头文件,直接调用相应的接口就可以了。
如果没有头文件就得使用dlopen dlsym
不过.so文件你可以直接copy到/libs/armeabi/下面
然后代码中使用绝对路径dlopen ("/data/data/lib/libxxx.so )
#13
参照实例:
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE :=hello-world
LOCAL_SRC_FILES :=hello-world.c
LOCAL_SHARED_LIBRARIES := hello-jni
LOCAL_LDLIBS += -llog
include $(BUILD_SHARED_LIBRARY)
相对应的c代码:
#include "string.h"
#include "jni.h"
#include "dlfcn.h"
#include <fcntl.h>
void * filehandle = NULL;
jint (*getResult)(JNIEnv *, jobject, jint, jint) =NULL;
jint JNICALL Java_com_example_hellojni_HelloAct_getInt(JNIEnv * env,
jobject thiz) {
jint result = 0;
filehandle = dlopen("libhello-jni.so", RTLD_LAZY);
if (filehandle) {
getResult = (jint (*)(JNIEnv *, jobject, jint, jint)) dlsym(filehandle,
"Java_com_example_hellojni_HelloJni_add");
if (getResult)
result = getResult(env, thiz, 3, 10);
dlclose(filehandle);
filehandle = NULL;
}
return result + 1;
}
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE :=hello-world
LOCAL_SRC_FILES :=hello-world.c
LOCAL_SHARED_LIBRARIES := hello-jni
LOCAL_LDLIBS += -llog
include $(BUILD_SHARED_LIBRARY)
相对应的c代码:
#include "string.h"
#include "jni.h"
#include "dlfcn.h"
#include <fcntl.h>
void * filehandle = NULL;
jint (*getResult)(JNIEnv *, jobject, jint, jint) =NULL;
jint JNICALL Java_com_example_hellojni_HelloAct_getInt(JNIEnv * env,
jobject thiz) {
jint result = 0;
filehandle = dlopen("libhello-jni.so", RTLD_LAZY);
if (filehandle) {
getResult = (jint (*)(JNIEnv *, jobject, jint, jint)) dlsym(filehandle,
"Java_com_example_hellojni_HelloJni_add");
if (getResult)
result = getResult(env, thiz, 3, 10);
dlclose(filehandle);
filehandle = NULL;
}
return result + 1;
}