为 Android 编译 C++ 库时如何修复此链接器错误?

问题描述 投票:0回答:1

我目前正在开发一个应用程序,可以处理数据并根据该数据制作 3D 对象。为此,我正在使用外部库:openMVG。我已经使用 cmake 构建了该库,在编译和链接阶段我收到此错误:

ld.lld: error: undefined symbol: __android_log_write
>>> referenced by logging.h:204 (/home/2North/gitdwnl/openMVG/src/third_party/ceres-solver/internal/ceres/miniglog/glog/logging.h:204)
>>>               sfm_data_BA_ceres.cpp.o:(MessageLogger::~MessageLogger()) in archive ../../Android-aarch64-Release/libopenMVG_sfm.a
>>> referenced by logging.h:208 (/home/2North/gitdwnl/openMVG/src/third_party/ceres-solver/internal/ceres/miniglog/glog/logging.h:208)
>>>               sfm_data_BA_ceres.cpp.o:(MessageLogger::~MessageLogger()) in archive ../../Android-aarch64-Release/libopenMVG_sfm.a

logging.h 中第 204 -208 行周围的代码:

line:187 #ifdef ANDROID
    static const int android_log_levels[] = {
        ANDROID_LOG_FATAL,    // LOG(FATAL)
        ANDROID_LOG_ERROR,    // LOG(ERROR)
        ANDROID_LOG_WARN,     // LOG(WARNING)
        ANDROID_LOG_INFO,     // LOG(INFO), LG, VLOG(0)
        ANDROID_LOG_DEBUG,    // VLOG(1)
        ANDROID_LOG_VERBOSE,  // VLOG(2) .. VLOG(N)
    };

    // Bound the logging level.
    const int kMaxVerboseLevel = 2;
    int android_level_index = std::min(std::max(FATAL, severity_),
                                       kMaxVerboseLevel) - FATAL;
    int android_log_level = android_log_levels[android_level_index];

    // Output the log string the Android log at the appropriate level.
line:204    __android_log_write(android_log_level, tag_.c_str(), stream_.str().c_str());

    // Indicate termination if needed.
    if (severity_ == FATAL) {
line:208      __android_log_write(ANDROID_LOG_FATAL,
                          tag_.c_str(),
                          "terminating.\n");
    }

我尝试按照不同人工智能的建议添加此代码并重新制作库:

find_library(LOG_LIB log)

if(LOG_LIB)
    message(STATUS "Android log library found at: ${LOG_LIB}")
else()
    message(FATAL_ERROR "Android log library not found!")
endif()
target_link_libraries(openMVG_sfm PRIVATE ${LOG_LIB})

target_link_libraries(openMVG_sfm PRIVATE ${LOG_LIB})

但它给了我另一个错误:

CMake Error at CMakeLists.txt:406 (target_link_libraries):
  Cannot specify link libraries for target "openMVG_sfm" which is not built
  by this project.

我以前没有制作和编译android c++库(以及一般库)的经验,所以如果我可以提供更多信息,我会提供它(我只是不知道问题可能出在哪里)

android cmake compilation
1个回答
0
投票
The error undefined symbol: __android_log_write indicates that the linker cannot find the implementation for the __android_log_write function, which is part of the Android logging library (liblog).

You attempted to resolve this by adding the liblog library in your CMake configuration, but encountered another error indicating that openMVG_sfm is not built by your project. This typically means that openMVG_sfm is not recognized as a target in your CMake configuration.

Steps to Resolve
Ensure liblog is Available:
Ensure that the Android logging library (liblog) is available and correctly linked. liblog is a system library provided by Android, so you need to link against it.

Correct CMake Configuration:
Make sure your CMakeLists.txt file is correctly set up to include and link the Android libraries. Follow these steps:

Find and Link liblog:
Use CMake's built-in functionality to find and link the Android logging library.

Here’s a basic example of how to configure CMake for Android:

cmake_minimum_required(VERSION 3.10)
project(openMVG_sfm)

# Specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Set Android-specific variables
if (ANDROID)
  # Specify the Android log library
  find_library(LOG_LIB log)

  # Check if the library was found
  if (LOG_LIB)
    message(STATUS "Android log library found at: ${LOG_LIB}")
  else()
    message(FATAL_ERROR "Android log library not found!")
  endif()

  # Link the Android log library
  target_link_libraries(openMVG_sfm PRIVATE ${LOG_LIB})
endif()

Ensure openMVG_sfm is Defined:
Ensure that openMVG_sfm is a valid target in your CMake configuration. If openMVG_sfm is not a target you created, you might need to create or include it properly.

Example of adding a library target:

add_library(openMVG_sfm SHARED src/sfm_data_BA_ceres.cpp)

Check CMakeLists.txt Structure:
Verify that your CMakeLists.txt file is correctly structured and that openMVG_sfm is properly defined and built.

Example CMake configuration might look like this:

cmake_minimum_required(VERSION 3.10)
project(openMVG_sfm)

# Specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Define the library target
add_library(openMVG_sfm SHARED
  src/sfm_data_BA_ceres.cpp
  # other source files
)

# Find the Android log library
if (ANDROID)
  find_library(LOG_LIB log)
  if (LOG_LIB)
    message(STATUS "Android log library found at: ${LOG_LIB}")
  else()
    message(FATAL_ERROR "Android log library not found!")
  endif()
  target_link_libraries(openMVG_sfm PRIVATE ${LOG_LIB})
endif()

Build Configuration:
Make sure you are using the correct CMake build configuration for Android. When configuring CMake for an Android build, ensure that you have specified the Android NDK and toolchain files correctly.

Example command for building with CMake:
cmake -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
      -DANDROID_ABI=arm64-v8a \
      -DANDROID_NATIVE_API_LEVEL=21 \
      -G "Unix Makefiles" \
      -B build \
      -S .
© www.soinside.com 2019 - 2024. All rights reserved.