Android NDK:.o和.c文件的多个定义

时间:2015-06-10 15:32:47

标签: android c++ android-studio android-ndk java-native-interface

我想在Android Studio的app项目中使用java和本机代码实现一个库。每当我更改c ++代码时,我都必须重新编译c ++文件,以便更改在我的应用程序中生效。不幸的是,ndk-build命令没有完全编译......

每次运行此命令时,它都会编译所有c ++文件,当它尝试创建.so文件时,我会收到很多错误,这些错误看起来都像这样(只是与其他类一起)。我所做的更改非常简单,并没有添加任何c ++包含标题等。

/Users/jenny/appproject/libraryWithNativeCode/src/main/obj/local/armeabi/objs/libraryWithNativeCode-mobile-example-app/src/WorldPins/WorldPinMessage.o: In function `_STLP_alloc_proxy':
/Users/jenny/NDK/android-ndk-r10e/sources/cxx-stl/stlport/stlport/stl/_string.c:647: multiple definition of `ExampleApp::WorldPins::WorldPinMessage::FocussedModel() const'
/Users/jenny/appproject/libraryWithNativeCode/src/main/obj/local/armeabi/objs/libraryWithNativeCode-mobile-example-app/src/WorldPins/WorldPinMessage.o:/Users/jenny/NDK/android-ndk-r10e/sources/cxx-stl/stlport/stlport/stl/_string.c:647: first defined here

我的Android.mk和Application.mk如下所示

Android.mk

LOCAL_PATH := $(call my-dir)

$(info TARGET_ARCH_ABI is $(TARGET_ARCH_ABI))

$(info LOCAL_PATH is $(LOCAL_PATH))

PREBUILT_LIBS := $(LOCAL_PATH)/../libs/libraryWithNativeCode/prebuilt/android-$(TARGET_ARCH_ABI)

include $(CLEAR_VARS)
LOCAL_MODULE := libraryWithNativeCode-sdk-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libraryWithNativeCode-sdk.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := png-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libpng.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../libs/libraryWithNativeCode/png
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := curl-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libcurl.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../libs/libraryWithNativeCode/curl/android-$(TARGET_ARCH_ABI)
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := ssl-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libssl.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := crypto-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libcrypto.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := http-parser-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libhttp-parser.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../libs/libraryWithNativeCode/http-parser
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := jpeg-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libjpeg.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := turbojpeg-lib
LOCAL_SRC_FILES := $(PREBUILT_LIBS)/libturbojpeg.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../libs/libraryWithNativeCode/jpeg-turbo
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE := libraryWithNativeCode-mobile-example-app
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2 -lz -lm
LOCAL_LDLIBS += -fuse-ld=bfd
LOCAL_STATIC_LIBRARIES := libraryWithNativeCode-sdk-lib png-lib curl-lib ssl-lib crypto-lib http-parser-lib jpeg-lib turbojpeg-lib android_native_app_glue ndk_helper

LOCAL_CFLAGS += -Wall -Wno-unknown-pragmas -Wno-sign-compare -Wno-format-security -Wno-reorder
#LOCAL_CFLAGS += -Werror

ifdef COMPILE_CPP_11
  $(info Configured for C++11)
  LOCAL_CPPFLAGS += -DCOMPILE_CPP_11=1 -std=c++11
else
  $(info Configured for C++0x)
endif

os_name:=$(shell uname -s)

get_android_cpp_files_cmd := find $(LOCAL_PATH) -type f  -iname "*.cpp"
get_android_includes_cmd  := find $(LOCAL_PATH) -type d
get_shared_cpp_files_cmd  := find $(LOCAL_PATH)/src -type f  -iname "*.cpp"
get_shared_includes_cmd   := find $(LOCAL_PATH)/src -type d
get_platform_includes_cmd := find $(LOCAL_PATH)/../libs/libraryWithNativeCode/platform -type d ! -path "*/OSX/*" ! -path "*/iOS/*"


ifeq ($(os_name),Darwin)
    cppfiles := ${shell ${get_android_cpp_files_cmd}}
    cppfiles += ${shell ${get_shared_cpp_files_cmd}}

    includes := ${shell ${get_android_includes_cmd}}
    includes += ${shell ${get_shared_includes_cmd}}
    includes += ${shell ${get_platform_includes_cmd}}
else
    # assume windows if not specified for now (due to no uname)
    cppfiles := ${shell sh -c '${get_android_cpp_files_cmd}'}
    cppfiles += ${shell sh -c '${get_shared_cpp_files_cmd}'}

    includes := ${shell sh -c '${get_android_includes_cmd}'}
    includes += ${shell sh -c '${get_shared_includes_cmd}'}
    includes += ${shell sh -c '${get_platform_includes_cmd}'}
endif


LOCAL_SRC_FILES := $(cppfiles:$(LOCAL_PATH)/%=%)
LOCAL_C_INCLUDES := $(includes)

LOCAL_C_INCLUDES += $(LOCAL_PATH)/../libs/libraryWithNativeCode/rapidjson
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../libs/libraryWithNativeCode/rapidjson/internal

include $(BUILD_SHARED_LIBRARY)

$(call import-module,android/native_app_glue)
$(call import-module,android/ndk_helper)

Application.mk

APP_PLATFORM := android-12
APP_STL := stlport_shared
APP_ABI := armeabi,armeabi-v7a,arm64-v8a

任何人都知道如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

我认为问题的根本原因是您为了编译而多次包含相同的文件,因为get_android_cpp_files_cmd(find $(LOCAL_PATH) -type f -iname "*.cpp")已经返回了由get_shared_cpp_files_cmd({{1}返回的文件因为find $(LOCAL_PATH)/src -type f -iname "*.cpp"是递归的。

此外,这与您的问题无关,但我想知道您为什么要为armeabi,armeabi-v7a,arm64-v8a和针对android-12 +进行编译? honeycomb_mr1是一个非常机密的版本,为什么不直接针对kitkat +,还包括你的列表中的x86 arch?

答案 1 :(得分:0)

如果你仍然遇到问题,即使在ph0b的解决方案之后,我也遇到了同样的情况。解决此问题的一种简单方法是清除 Android Studio 中的项目(构建 - >清理项目)。

要更详细的解决方案,请参阅我的回答here

希望它有所帮助!