Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, CMake and Static Linking

In converting a library project build from Android.mk to CMakeLists.txt following the Google hello-libs example and I have encountered problems avoiding undefined references. I'm using an open source project called DCMTK and compiling DCMTK and ICONV to static libraries that I then link in. Building using the Android.mk works via ndk-build and building them with gradle and the following CMakeLists.txt works when I add each static library to the target_link_libraries. However, I wanted to use add_library to do the job and tried

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 )

for each library and then adding them to target_link_libraries. This resulted in a ton of undefined references. I also tried set_target_properties by grouping the static libraries into one target, but this didn't work either.

Is the order of dependency linking different in some way or the way CMake handles these libraries different using the add_library set_target_properties method?

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} )
like image 811
Cameron Lowell Palmer Avatar asked Jan 27 '17 11:01

Cameron Lowell Palmer


Video Answer


1 Answers

While I'm unsure about adding the IMPORTED_LOCATION of all the static libraries en masse, breaking the work dependency order into libraries does work. You could improve this code quite easily with a for loop or writing a function or both.

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} )
like image 98
Cameron Lowell Palmer Avatar answered Oct 23 '22 17:10

Cameron Lowell Palmer