Problem in compiling android-ndk code
I am facing an issue and would like开发者_运维问答 to know why this is happening.
I have a project which consists of a Java
file and some JNI & C++
code files. I am building JNI and C++
code through cygwin
which is part of android ndk-tools. Below is my Android.mk
file
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := cube
LOCAL_CFLAGS := -DANDROID_NDK
LOCAL_SRC_FILES := Testing.cpp
LOCAL_LDLIBS := -lGLESv1_CM
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := jni_opengl
LOCAL_SRC_FILES := jni_openglcube.cpp
LOCAL_SHARED_LIBRARIES := cube
include $(BUILD_SHARED_LIBRARY)
In script above cube
module is written in standard C++
which do some opengl
stuff and jni_opengl
is written in JNI
. Above script compiles fine but when I change line 8 include $(BUILD_SHARED_LIBRARY)
to include $(BUILD_STATIC_LIBRARY)
I start getting error regarding opengl methods not found in Testing.cpp
.
My question is why in case of shared library
, android is able to find the references of opengl
related method and why not when I change it to static library
?
Symbols needed in shared libraries are sometimes looked up at runtime. So, I think you run into the same problem, but now at runtime not link time. See if the code with the shared library runs.
I don't think you need to have:
LOCAL_LDLIBS := -lGLESv1_CM
for your static library.
You will need to move it to your shared library section. The static section will be fine without it.
Don't forget to change:
LOCAL_SHARED_LIBRARIES := cube
to
LOCAL_STATIC_LIBRARIES := cube
A static library is just a bunch of .o files appended together, so it doesn't get 'linked' in the same way a shared library does.
Your final Android.mk should look something like this:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libcube
LOCAL_CFLAGS := -DANDROID_NDK
LOCAL_SRC_FILES := Testing.cpp
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := jni_opengl
LOCAL_SRC_FILES := jni_openglcube.cpp
LOCAL_STATIC_LIBRARIES := libcube
LOCAL_LDLIBS := -lGLESv1_CM -ldl -llog
include $(BUILD_SHARED_LIBRARY)
精彩评论