How to fix this linker error while compiling a C++ library for android?

  Kiến thức lập trình

I am currently developing an app that can proceed data and make 3D objects from that data. For that i am using an external library : openMVG. I’ve built that library with cmake and on the compiling & linking stage i got this error:

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

Code around line 204 -208 in logging.h:

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");
    }

I’ve tried adding this code as different AI’s have suggested and remake the library:

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})

but it gave me another error:

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

I have no previous experience of making and compiling android c++ libs (and libs in general), so if I can provide some more information, I’ll provide it (I just don’t know where the problem might be)

New contributor

Nikita is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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 .

New contributor

Mark Joseph Pinlac xWF is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT