0

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.

  • Firstly, I build it by using BUILDING.md (as four ANDROID_ABI: arm64-v8a, armeabi-v7a, x86, x86-64).
  • Secondly, I prepare in my project src/main/cpp folder libjpeg and put in ANDROID_ABI folders libjpeg.a static libraries.

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.

Kevin
  • 16,549
  • 8
  • 60
  • 74
M. Liver
  • 121
  • 6
  • What do you mean by "*I can't import library in my cpp file*"...? What does this mean? How do you know you can't import it? What happens, or doesn't happen? Can you give an example in your question post? – Kevin Jun 16 '20 at 20:11
  • When I add `#include ` to my code I have compilation error. – M. Liver Jun 16 '20 at 20:15
  • Thanks, can you add the complete error message to your question post? – Kevin Jun 16 '20 at 21:38

1 Answers1

1

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})
Kevin
  • 16,549
  • 8
  • 60
  • 74
  • Which exactly files I need to copy to include? I copied all headers from sources (downloaded from github) and from all headers from any (one of ANDROID_ABI) build directory -- is it correct? – M. Liver Jun 17 '20 at 05:21
  • @M.Liver Your question post didn't specify which repository you used, so I'm not sure. It is probably best to simply change the CMake path for `INTERFACE_INCLUDE_DIRECTORIES` to point to the directory *containing* the include files, instead of copying around individual files. – Kevin Jun 17 '20 at 12:36
  • Thank you for your answers. – M. Liver Jun 17 '20 at 16:34