androidc++cmakeandroid-ndklibjpeg-turbo

How to add libjpeg-turbo Android NDK as static library


I want to use libjpeg-turbo (exactly only libjpeg) in my Android NDK project. I can't find how to completely add the library to my project.

Next I add to CMakeLists.txt:

add_library( libjpeg STATIC IMPORTED )
set_target_properties( libjpeg
        PROPERTIES
        IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libjpeg/${ANDROID_ABI}/libjpeg.a )
# and
target_link_libraries(
    native-lib
    libjpeg
    ${log-lib})

Below is my whole CMakeLists.txt:

cmake_minimum_required(VERSION 3.4.1)

add_library( libjpeg STATIC IMPORTED )
set_target_properties( libjpeg
        PROPERTIES
        IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libjpeg/${ANDROID_ABI}/libjpeg.a )


include_directories(src/main/cpp/rapidjson/)
include_directories(src/main/cpp/Eigen)

file(GLOB CPP_FILES "src/main/cpp/*.cpp")
add_library(
        native-lib
        SHARED

        native-lib.cpp
        common.cpp
        archive.cpp
        crc32.cpp
        image.cpp
        read_manifest.cpp
        sensors.cpp
        thumbnail.cpp
        upf.cpp
        upf-toolkit.cpp
        write_manifest.cpp
        write_upf.cpp
        )
find_library(log-lib log)
target_link_libraries(native-lib libjpeg ${log-lib})

I have no building errors, but I can't include the libjpeg header in my cpp file.


Solution

  • You receive the compile error because your CMake code doesn't specify the location of libjpeg header files. You can specify the directory containing the libjpeg headers by setting the INTERFACE_INCLUDE_DIRECTORIES property for the imported libjpeg target.

    add_library( libjpeg STATIC IMPORTED )
    set_target_properties( libjpeg
            PROPERTIES
            IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libjpeg/${ANDROID_ABI}/libjpeg.a
            INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/libjpeg/include
    )
    

    Note: You may have to modify the path to match where these headers reside on your machine.

    With a couple other nit-picky notes (unrelated to the error), your updated CMake file may look something like this:

    cmake_minimum_required(VERSION 3.4.1)
    
    # You should always put the project directive at the top of your root CMakeLists.txt file.
    project(MyProject)
    
    add_library( libjpeg STATIC IMPORTED )
    set_target_properties( libjpeg
            PROPERTIES
            IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libjpeg/${ANDROID_ABI}/libjpeg.a
            INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/libjpeg/include
    )
    
    # You can list multiple directories in one include_directories() call.
    include_directories(
        src/main/cpp/rapidjson/
        src/main/cpp/Eigen
    )
    
    # Looks like this isn't used. Maybe you can remove it.
    file(GLOB CPP_FILES "src/main/cpp/*.cpp")
    
    add_library(
            native-lib
            SHARED
    
            native-lib.cpp
            common.cpp
            archive.cpp
            crc32.cpp
            image.cpp
            read_manifest.cpp
            sensors.cpp
            thumbnail.cpp
            upf.cpp
            upf-toolkit.cpp
            write_manifest.cpp
            write_upf.cpp
            )
    find_library(log-lib log)
    
    # Always place the scoping argument (e.g. PUBLIC, PRIVATE) in this call.
    target_link_libraries(native-lib PUBLIC libjpeg ${log-lib})