CMake not finding .so library - android-studio

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.

Related

CXX1405 cmake exception while building android project

Got error when I build the project like in the below. I tried so many things but never succeeded. I'm using m1 MacBook. Is this related with this error ?
[CXX1405] exception while building Json A problem occurred starting process 'command '/Users/serhat/Library/Android/sdk/cmake/3.18.1/bin/cmake''
in build.gradle:
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
and this is CmakeList.txt :
# For more information about using CMake with Android Studio,read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
I fixed this issue :
softwareupdate --install-rosetta

CMake not finding Qt5 when trying to create Qt5 Project in CLion on Linux

I am getting the following CMake error:
CMake Error at CMakeLists.txt:23 (find_package):
By not providing "FindQt5.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Qt5", but
CMake did not find one.
Could not find a package configuration file provided by "Qt5" with any of
the following names:
Qt5Config.cmake
qt5-config.cmake
Add the installation prefix of "Qt5" to CMAKE_PREFIX_PATH or set "Qt5_DIR"
to a directory containing one of the above files. If "Qt5" provides a
separate development package or SDK, be sure it has been installed.
In my CMakeLists.txt, there is this line:
if (NOT CMAKE_PREFIX_PATH)
message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it "
"(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)")
endif ()
So I did what it says and set my PREFIX_PATH to "/usr/include/x86_64-linux-gnu/qt5/".
However, the error I get says that CMake cannot find "qt5-config.cmake", which is located at "/usr/lib/x86_64-linux-gnu/cmake/Qt5" and not "/usr/include/x86_64-linux-gnu/qt5/", so I tried setting the "Qt5_DIR" to that directory (since it says in the error to set it to a dir that contains the above files) but still without success. Any ideas why it cant find the files it needs?
edit:
my CMakeLists.txt:
cmake_minimum_required(VERSION 3.19)
project(untitled1)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_PREFIX_PATH "/usr/lib/x86_64-linux-gnu/cmake/Qt5/")
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(QT_VERSION 5)
set(REQUIRED_LIBS Core Gui Widgets)
set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets)
add_executable(${PROJECT_NAME} main.cpp)
if (NOT CMAKE_PREFIX_PATH)
message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it "
"(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)")
endif ()
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})
note that the file has been auto-generated by CLion, all I did was change the path according to what is specified in the if-Block

cmake error while building OpenCV in native on Android Studio

I am trying to compile OpenCV in native on Android Studio. I am trying to follow : https://sriraghu.com/2017/03/11/opencv-in-android-an-introduction-part-1/
I was succesfully able to do it by following these steps on Windows. However on Ubuntu, its not working. The error is :
Execution failed for task ‘:app:externalNativeBuildDebug’.
Build command failed. Error while executing process /home/user/Android/Sdk/cmake/3.6.4111459/bin/cmake with arguments {–build
/home/user/AndroidStudioProjects/project_folders/app/.externalNativeBuild/cmake/debug/arm64-v8a
–target native-lib}
ninja: error: ‘../../../../jniLibs/src/main/jniLibs/arm64-v8a/libopencv_java3.so’,
needed by
‘../../../../build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so’,
missing and no known rule to make it
The CMakeLists.txt file is:
cmake_minimum_required(VERSION 3.4.1)
set(pathToOpenCV /media/user/Stuff/Softwares/OpenCV-android-sdk/sdk/native)
include_directories(${pathToOpenCV}/jni/include)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATIONS /home/user/cmu/android-apps/OpenCV_app_June23/OpenCV_Android/app/src/main/jniLibs/arm64-v8a/libopencv_java3.so)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} lib_opencv)
In native-lib.cpp, I am able to add #include without any error prompts in Android Studio.
My previous searches suggest that there is something wrong with the paths. I have checked that the paths are correct, or is there a different way to put the path in Ubuntu?

CMake: "add_library IMPORTED" check if the lib exists

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.

cmake ignoring include directory when cross compiling

I am perplexed and would like your help.
I am cross compiling a project that requires ifaddrs.h, but no matter how I try to specify the location of the file, it never finds it. It is located at /usr/include/ifaddrs.h and I have used the code below to try to point to it. (Granted this is the host system's file, but that should not matter in this context. I have also put the file in the target systems include directory: /tmp/toolchain/include with no different results). And the message call does print /usr/include
If, however, I put the file in ${CMAKE_CURRENT_SOURCE_DIR}/../include
it is found. It is there, I can see it -- why not cmake?
FIND_FILE( IFADDRS ifaddrs.h PATH /usr/include )
get_filename_component(if_path ${IFADDRS} PATH)
message( "IFADDRS = " ${if_path} )
include_directories( ${if_path} )
include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/../include )
Changing the code above to this:
include_directories( BEFORE SYSTEM /usr/include )
and running make VERBOSE=1 gives this:
[ 18%] Building C object CMakeFiles/cpcommon_lib.dir/eui48.c.o
/tmp/toolchain/arm-linux-uclibc/bin/gcc -DPJ_AUTOCONF=1 -DPJ_IS_BIG_ENDIAN=0 -DPJ_IS_LITTLE_ENDIAN=1 -I/home/bp/MySnapCam/projects/Communication/trunk/cpcommon/src/../include -I/home/bp/MySnapCam/projects/Communication/trunk/cpcommon/src/../../pjproject/pjlib-util/include -I/home/bp/MySnapCam/projects/Communication/trunk/cpcommon/src/../../pjproject/pjlib/include -I/home/bp/MySnapCam/projects/Communication/trunk/cpcommon/src/../../pjproject/pjnath/include -I/usr/include/w32api -I/usr/X11R6/include -I/usr/include/X11 -I/usr/pkg/include -I/opt/csw/include -I/opt/include -I/usr/openwin/include -I/home/bp/MySnapCam/projects/Communication/trunk/cpcommon/src/../../cjson -o CMakeFiles/cpcommon_lib.dir/eui48.c.o -c /home/bp/MySnapCam/projects/Communication/trunk/cpcommon/src/eui48.c
In file included from /home/bp/MySnapCam/projects/Communication/trunk/cpcommon/src/eui48.c:26:
/home/bp/MySnapCam/projects/Communication/trunk/cpcommon/src/impl/eui48_unix.c:14:21: ifaddrs.h: No such file or directory
There is no /usr/include/ in the include paths. It is also not in the flags.make files either.

Resources