Friday, September 2, 2022

[SOLVED] Cmake error : set_target_properties Can not find target to add properties to: lib_opencv

Issue

I am tying use log for debugging in my cmake ndk project,but when i am trying to add the log-lib library it gives error on compilation time:

CMake Error at CMakeLists.txt:21 (set_target_properties):
    set_target_properties Can not find target to add properties to: lib_opencv

If i remove the set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java4.so) ,it will compile successfully.

my cmakelist :

cmake_minimum_required(VERSION 3.10.2)
project(OPENcv_app)

include_directories(../include)

# opencv
set(OpenCV_STATIC ON)

set(OpenCV_DIR c:/tools/OpenCV-android-sdk/sdk/native/jni)
find_package(OpenCV REQUIRED)

add_library( # Sets the name of the library.
    native_opencv SHARED
    lib_opencv SHARED IMPORTED

    # Provides a relative path to your source file(s).
    ../ios/Classes/ArucoDetector/ArucoDetector.cpp
    ../ios/Classes/native_opencv.cpp
)

set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java4.so)

target_include_directories(
    native_opencv PRIVATE
    ../ios/Classes/ArucoDetector
)

find_library(log-lib log)

target_link_libraries( # Specifies the target library.
    native_opencv
    ${OpenCV_LIBS}
    lib_opencv
    ${log-lib}
)

Solution

A library cannot be both imported and non-imported and you cannot declare 2 targets with the same add_library command. You need to separate those:

# create native_opencv target built as part of this project
add_library(native_opencv SHARED

    # Provides a relative path to your source file(s).
    ../ios/Classes/ArucoDetector/ArucoDetector.cpp
    ../ios/Classes/native_opencv.cpp
)

# create imported library lib_opencv
add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java4.so)


Answered By - fabian
Answer Checked By - Candace Johnson (WPSolving Volunteer)