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

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật