You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用ndk-build编译Android项目时出现Undefined reference错误求助

Troubleshooting "Undefined reference to xxx" Errors with ndk-build for GDAL on Windows

Sorry to hear you're stuck on these pesky undefined reference errors—they’re always a frustrating roadblock, especially after getting through the configure/make/make install grind. Let’s break down the most likely culprits and fixes for your setup:

1. Verify GDAL was cross-compiled for Android (not Windows native)

This is the most common pitfall: running ./configure & make & make install directly on Windows will build Windows-native libraries, not the ARM/x86 libraries Android needs. You must use the NDK’s cross-compilation toolchain to configure GDAL properly.

Here’s a corrected workflow:

  • First, set up environment variables pointing to your NDK toolchain (adjust paths and target architecture to match your project):
    export NDK=C:/path/to/your/android-ndk-r25c
    export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/windows-x86_64
    export TARGET=aarch64-linux-android
    export API=24 # Match your min Android SDK version
    export AR=$TOOLCHAIN/bin/llvm-ar
    export CC=$TOOLCHAIN/bin/$TARGET$API-clang
    export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
    export LD=$TOOLCHAIN/bin/ld
    export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
    export STRIP=$TOOLCHAIN/bin/llvm-strip
    
  • Run configure with cross-compilation flags and a prefix pointing to your project’s jni directory (so ndk-build can find the outputs):
    ./configure --host=$TARGET \
      --prefix=C:/path/to/your/project/jni/gdal-android \
      --with-sqlite3=no \ # Disable unneeded dependencies to simplify
      --with-curl=no \
      --disable-static \
      --enable-shared
    
  • Re-run make && make install—this will generate Android-compatible .so files and headers.

2. Fix your Android.mk/Application.mk configuration

ndk-build relies on these scripts to locate GDAL’s files. Double-check these settings:

Android.mk Example

LOCAL_PATH := $(call my-dir)

# Prebuilt GDAL library
include $(CLEAR_VARS)
LOCAL_MODULE := gdal
LOCAL_SRC_FILES := gdal-android/lib/libgdal.so # Path to your cross-compiled GDAL lib
include $(PREBUILT_SHARED_LIBRARY)

# Your project's shared library
include $(CLEAR_VARS)
LOCAL_MODULE := your_project_lib
LOCAL_SRC_FILES := your_cpp_source.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)/gdal-android/include # Path to GDAL headers
LOCAL_SHARED_LIBRARIES := gdal # Link against GDAL
include $(BUILD_SHARED_LIBRARY)

Application.mk Example

APP_ABI := armeabi-v7a arm64-v8a # Match the architecture you compiled GDAL for
APP_PLATFORM := android-24 # Match the API level used in configure
APP_STL := c++_shared # Ensure this matches GDAL's C++ standard library choice

3. Check for missing dependencies

GDAL links against many third-party libraries (SQLite, Proj, Curl, etc.). If your configure command enabled any of these but you didn’t cross-compile them for Android, the linker will fail to find their symbols. Fix this by:

  • Disabling unneeded dependencies in configure with --with-xxx=no
  • Cross-compiling required dependencies and adding them to your Android.mk as prebuilt libraries

4. Fix C/C++ name mangling issues

If you’re calling GDAL’s C functions from C++ code, wrap GDAL headers in extern "C" to prevent C++ name mangling (which breaks symbol lookup):

extern "C" {
#include "gdal.h"
#include "ogr_api.h"
}

5. Verify symbols exist in your GDAL library

Use the NDK’s llvm-nm tool to check if the missing symbol is actually present in your cross-compiled libgdal.so:

$TOOLCHAIN/bin/llvm-nm -D C:/path/to/gdal-android/lib/libgdal.so | grep "xxx"

If the symbol isn’t found, re-run configure with flags to enable the missing GDAL module.

If you can share the exact configure command you used and a snippet of the error logs, we can narrow this down even further!

内容的提问来源于stack exchange,提问作者Mahdi Nazari Ashani

火山引擎 最新活动