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} )
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} )
Related
I am trying to build a Library (MyLib) that has a static dependency to boost. However, when I am trying link MyLib to an Application I will get linking erorrs saying that boost functions cannot be found.
I did used the pointer to implementation idiom.
Conan installed the static lib of boost (I checked that).
The plattform is windows.
Does anyone has an idea what I am doing wrong?
Here is the CMakeLists.txt
cmake_minimum_required(VERSION 3.24)
set(TARGET_NAME MyLib)
project(${TARGET_NAME}Project)
include(${CMAKE_CURRENT_LIST_DIR}/build/conanbuildinfo.cmake)
conan_basic_setup()
set(
SRC_FILES
src/FileA.cpp
src/FileB.cpp
src/FileC.cpp
)
add_library(${TARGET_NAME} ${SRC_FILES})
target_include_directories(
${TARGET_NAME}
PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include/public
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include/private
)
target_link_libraries(${TARGET_NAME} PRIVATE ${CONAN_LIBS_STATIC})
set_property(TARGET ${TARGET_NAME} PROPERTY CXX_STANDARD 17)
install(TARGETS ${TARGET_NAME} DESTINATION lib)
This is the erorr I am getting:
Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol "public: class boost::log::v2s_mt_nt6::attribute_value_set::const_iterator __cdecl boost::log::v2s_mt_nt6::attribute_value_set::end(void)const " (?end#attribute_value_set#v2s_mt_nt6#log#boost##QEBA?AVconst_iterator#1234#XZ) Main C:\dev\TestProjects\VSLog\Main\CppLogLib.lib(ConsoleLogWrapper.obj) 1
Many thanks in advance.
I tried with CMake setting like
set(Boost_USE_STATIC_LIBS ON) or set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS "OPT:NOREF") but it did not help.
I also try to build MyLib using sln (and Boost downloaded from boost.org) and here I also get a linking error:
Error LNK1104 cannot open file 'libboost_log-vc143-mt-gd-x64-1_81.lib'
For my diploma project, I'm trying to use Android Studio to port my c++ project to android. My c++ project includes my own code and library which was developed before me.
Firstly I was tried to compile the source code of this library to a shared android library. I successfully did it. But when I am compiling the code which uses this library, cmake tells me that my library is incompatible for x86 architecture (I think for others will be too).
When I was compiling the library I use this code for different ABI includes:
if (${ANDROID_ABI} STREQUAL "armeabi-v7a")
include_directories(${ANDROID_SYSROOT}/usr/include/arm-linux-androideabi)
elseif (${ANDROID_ABI} STREQUAL "x86_64")
include_directories(${ANDROID_SYSROOT}/usr/include/x86_64-linux-android)
elseif (${ANDROID_ABI} STREQUAL "x86")
include_directories(${ANDROID_SYSROOT}/usr/include/i686-linux-android)
elseif (${ANDROID_ABI} STREQUAL "arm64-v8a")
include_directories(${ANDROID_SYSROOT}/usr/include/aarch64-linux-android)
endif()
I don't know how to fix that. It looks like I need to link any ABI dependent library, but I don't know how I can understand which library I need. "My" library has many files and it would be hard to just trace all dependencies. So how I can fix that (sure after building "my" library I have *.so for all ABIs and I am linking they to main code using ${ANDROID_ABI})?
I have 2 modules in my Android Studio project: library and application. The library builds well, but I can't link it to the application because cmake tells me that library is an incompatible target. After changing cmakes I got rid of ABI cmake block, but the library still doesn't want to link.
App cmake:
cmake_minimum_required(VERSION 3.4.1)
add_library(native-lib
SHARED
src/main/cpp/native-lib.cpp )
find_library(log-lib
log )
add_library( libacheron SHARED IMPORTED )
set(lib_src_DIR ${CMAKE_CURRENT_LIST_DIR}/../acheron_lib/build/Debug/acheron)
set_target_properties(libacheron
PROPERTIES IMPORTED_LOCATION
${lib_src_DIR}/libacheron.so)
include_directories(${CMAKE_CURRENT_LIST_DIR}/../acheron_lib/temp/include)
target_link_libraries( native-lib
libacheron
${log-lib})
lib main cmake:
cmake_minimum_required(VERSION 3.3)
set(ACHERON_GLOBAL_ROOT ${CMAKE_CURRENT_LIST_DIR})
set(ACHERON_GLOBAL_BUILD_DIRECTORY "${ACHERON_GLOBAL_ROOT}/build")
function(acheron_add_subprojects proj_dir_list)
foreach(proj_dir ${proj_dir_list})
add_subdirectory(${proj_dir})
endforeach()
endfunction()
acheron_add_subprojects("${ACHERON_GLOBAL_ROOT}/src/main/acheron")
lib other cmake:
cmake_minimum_required(VERSION 3.3)
project(acheron)
set(CMAKE_CURRENT_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR})
macro(acheron_set_build_directory build_dir)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${build_dir})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${build_dir})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${build_dir})
if(MSVC)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${build_dir})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${build_dir})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${build_dir})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${build_dir})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${build_dir})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${build_dir})
endif(MSVC)
endmacro()
acheron_set_build_directory(${ACHERON_GLOBAL_BUILD_DIRECTORY}/${CMAKE_BUILD_TYPE}/${PROJECT_NAME})
function(acheron_get_files_from_list out_file_list in_list)
set(file_list "")
foreach(in_item ${in_list})
if(NOT IS_DIRECTORY ${in_item})
list(APPEND file_list ${in_item})
endif()
endforeach()
set(${out_file_list} ${file_list} PARENT_SCOPE)
endfunction()
function(acheron_get_all_files_recursively out_file_list folder)
file(GLOB_RECURSE all_list ${folder}/*)
acheron_get_files_from_list(file_list "${all_list}")
set(${out_file_list} ${file_list} PARENT_SCOPE)
endfunction()
acheron_get_all_files_recursively(acheron_files ${CMAKE_CURRENT_SOURCE_DIR})
function(acheron_get_src_regex_pattern out_pattern)
set(${out_pattern} ".*\\.(((C|c)(P|p)(P|p)))$" PARENT_SCOPE)
endfunction()
acheron_get_src_regex_pattern(src_pattern)
function(acheron_get_filtered out_list regex_pattern in_list)
set(temp_list "")
foreach(in_item ${in_list})
if(${in_item} MATCHES ${regex_pattern})
list(APPEND temp_list ${in_item})
endif()
endforeach()
set(${out_list} ${temp_list} PARENT_SCOPE)
endfunction()
acheron_get_filtered(acheron_files ${src_pattern} "${acheron_files}")
function(acheron_add_source_groups root_dir src_list)
foreach(src_file ${src_list})
get_filename_component(src_name ${src_file} NAME)
string(REPLACE ${root_dir}/ "" group_name ${src_file})
string(REPLACE /${src_name} "" group_name ${group_name})
string(REPLACE "/" "\\" group_name ${group_name})
if(NOT ${group_name} STREQUAL ${src_name})
source_group(${group_name} FILES ${src_file})
endif()
endforeach()
endfunction()
acheron_add_source_groups(${CMAKE_CURRENT_SOURCE_DIR} "${acheron_files}")
add_library(${PROJECT_NAME} SHARED "${acheron_files}")
I finally did it! After I had rewritten cmakes files I was close to correct decision. I miss ABI dependence. I have a little corrected my cmakes:
app cmake:
set(lib_src_DIR ${CMAKE_CURRENT_LIST_DIR}/../acheron_lib/build/${CMAKE_BUILD_TYPE}/acheron)
set_target_properties(libacheron
PROPERTIES IMPORTED_LOCATION
${lib_src_DIR}/${ANDROID_ABI}/libacheron.so)
lib other cmake (I remove MSVC block just because of it useless for android build):
macro(acheron_set_build_directory build_dir)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${build_dir}/${ANDROID_ABI})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${build_dir}/${ANDROID_ABI})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${build_dir}/${ANDROID_ABI})
endmacro()
You don't need to add that block of CMake manually (it's also far from complete). Follow the docs on using CMake with the NDK and it'll handle handle the details for you: https://developer.android.com/studio/projects/add-native-code
I am importing a static lib in CMAKE using add_library.
The lib imports fine.But I also want to verify that.So I do this:
add_library(MYLIB STATIC IMPORTED)
set_target_properties(MYLIB PROPERTIES IMPORTED_LOCATION path/to/mylib.a)
#if(NOT MYLIB)
# message(FATAL_ERROR "MYLIB library not found")
#endif()
It always returns false, even when the path is correct and the lib is imported ok.How can I check that the lib is imported?
Using Cmake 3.4.1
Command add_library() creates a target, not a variable.
Target existence can be checked with TARGET keyword:
add_library(MYLIB STATIC IMPORTED)
#...
if(NOT TARGET MYLIB)
# Target is not exist
endif()
Note, that existence of the library target doesn't mean existence of the library file. Existence of the file should use EXISTS keyword:
if(EXISTS <path>)
Since you are failing/stopping processing anyway, then you could check it is imported by trying to use the library!
If you actually want to check before you import, then something like this may be appropriate:
find_library(MYLIB
NAMES mylib
PATHS
path/to/mylib.a
DOC "Find library mylib"
NO_DEFAULT_PATH)
if(NOT MYLIB)
message(FATAL_ERROR "MYLIB library not found")
endif()
You can remove NO_DEFAULT_PATH if you don't mind CMake looking in all the many default locations first before searching your stated PATHS.
I've read several threads (and docs) on including .so libraries with CMake, but it doesn't appear to me that I've fallen into the errors that I've read about, namely:
supplying an incorrect library name
specifying an incorrect path
So what am I doing amiss? I'm working in Android Studio, and I have a cmake file in my app directory. It can find some things but not the .so file in question, whose path is:
app/src/main/cpp/libusb-android-open2/android/libs/armeabi/libusb1.0.so
Here's my cmake file:
# This succeeds
add_library( demo-libusb SHARED src/main/cpp/demo-libusb.cpp )
# This succeeds
find_library( log-lib log )
if (log-lib)
message(WARNING "HAS loglib") # This message gets printed
else(log-lib)
message(SEND_ERROR "NO loglib")
endif(log-lib)
# This fails
link_directories( src/main/cpp/libusb-android-open2/android/libs/armeabi )
find_library( usb-lib NAMES usb1.0 libusb1.0.so )
if ( usb-lib )
message(WARNING "HAS libusb")
else( usb-lib )
message(SEND_ERROR "NO libusb") # This message gets printed
endif( usb-lib )
I have also tried:
find_library( usb-lib NAMES libusb1.0.so usb1.0 PATHS
src/main/cpp/libusb-android-open2/android/libs/armeabi
src/main/cpp/libusb-android-open2/android/libs/armeabi-v7a
)
Command link_directories doesn't affect on find_library search paths.
You may use PATH or HINT options for find_library, or set variables which are explicitely noted in its search algorithm.
I have a problem porting POCO 1.4.6p2 to ARM based platform. I have successfully maked all the POCO libraries and installed those using cross-compiling and a ToolChain from Karo Electronics for their TX28 board (http://www.karo-electronics.com/tx28.html).
However when I try to link to any of the dynamic release POCO libraries, f. ex. libPocoFoundation.so I get link errors like this:
Linking CXX executable hellodingo
/home/armsdk/projects/poco/install/lib/libPocoFoundation.so: undefined reference to Poco::Channel::setProperty(std::string const&, std::string const&)'
/home/armsdk/projects/poco/install/lib/libPocoFoundation.so: undefined reference toPoco::LoggingRegistry::channelForName(stlp_std::basic_string, stlp_std::allocator > const&) const'
/home/armsdk/projects/poco/install/lib/libPocoFoundation.so: undefined reference to Poco::SystemException::SystemException(std::string const&, int)'
/home/armsdk/projects/poco/install/lib/libPocoFoundation.so: undefined reference toPoco::RegularExpressionException::RegularExpressionException(std::string const&, int)'
...
However if I link instead to the dynamic debug version of the POCO libraries, f.ex. libPocoFoundationd.so, everything links without a problem.
This is also true if I try to build the POCO libraries and do not omit PageCompiler,PageCompiler/File2Page which are executables needing PocoFoundation and more POCO libraries.
I'm using version gcc and g++ version 4.7.2 (the cross compiler).
I'm using the STLport v. 5.2.1 when building the POCO libraries.
What can be causing this link-difference between the release and debug version of the POCO libraries?
PS: Even when using the debug version of the POCO libs when I add more libraries than just the foundation (for example the PocoZip library) , I start getting the same errors from PocoZip complaining about undefined reference from PocoZip to items in PocoFoundatoin.
Below is a CMAkeLists.txt that I use for building:
cmake_minimum_required(VERSION 2.8)
set(CMAKE_TOOLCHAIN_FILE /home/armsdk/projects/Toolchain-TX28.cmake)
project(hellodingo)
add_executable(hellodingo SmoDeviceI2C.cpp tca6424a.cpp main.cpp)
include_directories(/home/armsdk/projects/poco/install/include)
add_library(STLport SHARED IMPORTED)
add_library(PocoFoundation SHARED IMPORTED)
set_target_properties(STLport PROPERTIES IMPORTED_LOCATION "/home/armsdk/projects/STLport/install/arm-926ejs-linux-gnueabi-lib/libstlport_arm-linux-gcc.so")
set_target_properties(PocoFoundation PROPERTIES IMPORTED_LOCATION "/home/armsdk/projects/poco/install/lib/libPocoFoundationd.so")
set_target_properties(PocoUtil PROPERTIES IMPORTED_LOCATION "/home/armsdk/projects/poco/install/lib/libPocoUtild.so")
target_link_libraries(hellodingo STLport PocoFoundation PocoUtil)
install(TARGETS hellodingo RUNTIME DESTINATION bin)