我想在我的Android NDK项目中使用libjpeg-turbo(确切地说只有libjpeg(。我找不到如何将库完全添加到我的项目中。
- 首先,我使用
BUILDING.md
构建它(作为四个ANDROID_ABI
:arm64-v8a
、armeabi-v7a
、x86
、x86-64
(。 - 其次,我在我的项目中准备
src/main/cpp
文件夹libjpeg
并放入ANDROID_ABI
文件夹libjpeg.a静态库。
接下来我添加到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})
以下是我的整个CMakeList.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})
我没有构建错误,但我无法在我的 cpp 文件中包含 libjpeg 标头。
您会收到编译错误,因为您的 CMake 代码未指定libjpeg
头文件的位置。可以通过为导入的libjpeg
目标设置INTERFACE_INCLUDE_DIRECTORIES
属性来指定包含libjpeg
标头的目录。
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
)
注意:您可能需要修改路径以匹配这些标头在计算机上的驻留位置。
加上其他一些挑剔的笔记(与错误无关(,更新后的 CMake 文件可能如下所示:
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})