Android,CMake和静态链接

时间:2017-01-27 11:08:03

标签: android-ndk cmake

在Google hello-libs example之后将库项目构建从Android.mk转换为CMakeLists.txt,我遇到了避免未定义引用的问题。我正在使用一个名为DCMTK的开源项目,并将DCMTK和ICONV编译为我随后链接的静态库。使用Android.mk进行构建可以通过ndk-build进行构建并构建它们当我将每个静态库添加到gradle时,使用CMakeLists.txt和以下target_link_libraries。但是,我想使用add_library来完成这项工作并尝试

add_library( lib_dcmdata STATIC IMPORTED )
set_target_properties( lib_dcmdata
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmdata.a )

为每个库,然后将它们添加到target_link_libraries。这导致了大量未定义的引用。我还尝试通过将静态库分组到一个目标中来设置set_target_properties,但这也不起作用。

依赖关系的顺序是否以某种方式不同或者CMake使用add_library set_target_properties方法处理这些库的方式不同?

的CMakeLists.txt

cmake_minimum_required( VERSION 3.6 )
project( dicom-jni )

set( SOURCE_FILES
         dicom.cpp
         dicom.hpp )

add_library( lib_dcmtk STATIC IMPORTED )
set_target_properties( lib_dcmtk
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmrt.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmpstat.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmqrdb.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmwlm.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmdsig.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmnet.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmjpeg.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmjpls.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libcharls.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libijg16.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libijg12.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libijg8.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmimage.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmimgle.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libi2d.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmdata.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/liboflog.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libofstd.a )

add_library( lib_iconv STATIC IMPORTED )
set_target_properties( lib_iconv
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/iconv/${ANDROID_ABI}/lib/libiconv.a
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/iconv/${ANDROID_ABI}/lib/libcharset.a )

find_library( lib_jnigraphics jnigraphics )
find_library( lib_android-log log )

add_library( dicom-jni SHARED ${SOURCE_FILES} )

target_include_directories( dicom-jni PRIVATE
                                prebuilt_libs/dcmtk/${ANDROID_ABI}/include
                                prebuilt_libs/iconv/${ANDROID_ABI}/include
                                logger )

target_link_libraries( dicom-jni
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmrt.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmpstat.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmqrdb.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmwlm.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmdsig.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmnet.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmjpeg.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmjpls.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libcharls.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libijg16.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libijg12.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libijg8.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmimage.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmimgle.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libi2d.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmdata.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/liboflog.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libofstd.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/iconv/${ANDROID_ABI}/lib/libiconv.a
                           ${CMAKE_SOURCE_DIR}/prebuilt_libs/iconv/${ANDROID_ABI}/lib/libcharset.a
                           z
                           dl
                           stdc++
                           ${lib_jnigraphics}
                           ${lib_android-log} )

1 个答案:

答案 0 :(得分:4)

虽然我不确定添加所有静态库的IMPORTED_LOCATION,但是将工作依赖顺序分解为库确实有效。

cmake_minimum_required( VERSION 3.6 )
project( dicom-jni )

set( SOURCE_FILES
         dicom.cpp
         dicom.hpp )

add_library( lib_dcmrt STATIC IMPORTED )
set_target_properties( lib_dcmrt
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmrt.a )

add_library( lib_dcmpstat STATIC IMPORTED )
set_target_properties( lib_dcmpstat
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmpstat.a )

add_library( lib_dcmqrdb STATIC IMPORTED )
set_target_properties( lib_dcmqrdb
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmqrdb.a )

add_library( lib_dcmwlm STATIC IMPORTED )
set_target_properties( lib_dcmwlm
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmwlm.a )

add_library( lib_dcmdsig STATIC IMPORTED )
set_target_properties( lib_dcmdsig
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmdsig.a )

add_library( lib_dcmnet STATIC IMPORTED )
set_target_properties( lib_dcmnet
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmnet.a )

add_library( lib_dcmjpeg STATIC IMPORTED )
set_target_properties( lib_dcmjpeg
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmjpeg.a )

add_library( lib_dcmjpls STATIC IMPORTED )
set_target_properties( lib_dcmjpls
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmjpls.a )

add_library( lib_charls STATIC IMPORTED )
set_target_properties( lib_charls
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libcharls.a )

add_library( lib_ijg16 STATIC IMPORTED )
set_target_properties( lib_ijg16
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libijg16.a )

add_library( lib_ijg12 STATIC IMPORTED )
set_target_properties( lib_ijg12
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libijg12.a )

add_library( lib_ijg8 STATIC IMPORTED )
set_target_properties( lib_ijg8
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libijg8.a )

add_library( lib_dcmimage STATIC IMPORTED )
set_target_properties( lib_dcmimage
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmimage.a )

add_library( lib_dcmimgle STATIC IMPORTED )
set_target_properties( lib_dcmimgle
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmimgle.a )

add_library( lib_i2d STATIC IMPORTED )
set_target_properties( lib_i2d
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libi2d.a )

add_library( lib_dcmdata STATIC IMPORTED )
set_target_properties( lib_dcmdata
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libdcmdata.a )

add_library( lib_oflog STATIC IMPORTED )
set_target_properties( lib_oflog
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/liboflog.a )

add_library( lib_ofstd STATIC IMPORTED )
set_target_properties( lib_ofstd
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/dcmtk/${ANDROID_ABI}/lib/libofstd.a )

add_library( lib_iconv STATIC IMPORTED )
set_target_properties( lib_iconv
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/iconv/${ANDROID_ABI}/lib/libiconv.a )

add_library( lib_charset STATIC IMPORTED )
set_target_properties( lib_charset
                           PROPERTIES
                           IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt_libs/iconv/${ANDROID_ABI}/lib/libcharset.a )

find_library( lib_jnigraphics jnigraphics )
find_library( lib_android-log log )

add_library( dicom-jni SHARED ${SOURCE_FILES} )

target_include_directories( dicom-jni PRIVATE
                                prebuilt_libs/dcmtk/${ANDROID_ABI}/include
                                prebuilt_libs/iconv/${ANDROID_ABI}/include
                                logger )

target_link_libraries( dicom-jni
                           lib_dcmrt
                           lib_dcmpstat
                           lib_dcmqrdb
                           lib_dcmwlm
                           lib_dcmdsig
                           lib_dcmnet
                           lib_dcmjpeg
                           lib_dcmjpls
                           lib_charls
                           lib_ijg16
                           lib_ijg12
                           lib_ijg8
                           lib_dcmimage
                           lib_dcmimgle
                           lib_i2d
                           lib_dcmdata
                           lib_oflog
                           lib_ofstd
                           lib_iconv
                           lib_charset
                           z
                           dl
                           stdc++
                           ${lib_jnigraphics}
                           ${lib_android-log} )