Android NDK编译报错:protobuf模块重复定义问题求助
Let's break down what's causing this error and walk through the fix step by step:
Root Cause
Your Android.mk has a structural flaw: you started defining the ndktest module but never completed it (you skipped calling include $(BUILD_SHARED_LIBRARY) for it before moving to prebuilt libraries). Then, after setting up all prebuilt libraries, you added compiler/linker flags without first resetting the build environment with include $(CLEAR_VARS)—this caused those flags to get attached to the last prebuilt module (protobuf), triggering the "already defined" conflict.
Fixed Android.mk
Here's the corrected version with proper module separation and complete build cycles:
LOCAL_PATH:= $(call my-dir) # First define all prebuilt shared libraries include $(CLEAR_VARS) LOCAL_MODULE := c++_shared LOCAL_SRC_FILES := $(LOCAL_PATH)/inc/build/libc++_shared.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := armnn LOCAL_SRC_FILES := $(LOCAL_PATH)/inc/build/libarmnn.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := armnnTfParser LOCAL_SRC_FILES := $(LOCAL_PATH)/inc/build/libarmnnTfParser.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := protobuf LOCAL_SRC_FILES := $(LOCAL_PATH)/inc/build/libprotobuf.so include $(PREBUILT_SHARED_LIBRARY) # Now define your main ndktest module include $(CLEAR_VARS) LOCAL_MODULE := ndktest LOCAL_SRC_FILES := ndktest.cpp # Add compiler/linker flags and dependencies LOCAL_LDLIBS += -lm -llog -landroid LOCAL_CFLAGS += -DARM_NEON_64BIT -O3 -mfpu=neon -mcpu=kryo -std=c++14 -pie LOCAL_SHARED_LIBRARIES := opencv_java3 armnn armnnTfParser protobuf c++_shared LOCAL_C_INCLUDES := $(LOCAL_PATH)/inc/include # Finalize the main module build include $(BUILD_SHARED_LIBRARY)
Key Changes Explained
- Reordered module flow: We handle all prebuilt libraries first, then our custom
ndktestmodule. This keeps build logic isolated and avoids cross-contamination between modules. - Complete module cycles: Every module (prebuilt or custom) now follows the full required sequence:
include $(CLEAR_VARS)→ set module properties →include [BUILD/PREBUILT]_SHARED_LIBRARY. This ensures each module is properly closed before moving to the next. - Clean environment for main module: We added
include $(CLEAR_VARS)right before definingndktest, so all compiler/linker flags apply only to this module, not leftover from the last prebuilt library.
Quick Note for Application.mk
You’re using APP_STL := gnustl_static but including c++_shared as a prebuilt library—these might conflict. If you intend to use the prebuilt libc++_shared.so, consider changing APP_STL to c++_shared instead.
内容的提问来源于stack exchange,提问作者Parag Jain

