importing a cpp project build with cmake in android using ndk - android-ndk

I have a Cpp project that use OpenCV. I build it using cmake here is the CMakeLists.txt:
set(CMAKE_CXX_STANDARD 11)
find_package( OpenCV 3.1.0 REQUIRED core text)
include_directories( ${OpenCV_INCLUDE_DIRS})
set(OpenCV_LIBS opencv_core opencv_objdetect)
add_library(opencv lib/utils/opencv.cpp lib/utils/opencv.h)
set(SOURCES main.cpp )
add_executable( ${PROJECT_NAME} ${SOURCES} )
target_link_libraries( ${PROJECT_NAME} ${OpenCV_LIBS} )
target_link_libraries( ${PROJECT_NAME} opencv )
On Android studio I have some External Native Build Issues:
...
CMake Error at /home/benng/bin/opencv_working_dir/opencv-3.1.0/build/OpenCVConfig.cmake:86 (include):
include could not find load file:
/home/benng/bin/opencv_working_dir/opencv-3.1.0/build/OpenCVModules_armeabi.cmake
Call Stack (most recent call first):
src/CMakeLists.txt:5 (find_package)
OpenCV version : 3.1.0
...
I was wondering:
- if I had to use the OpenCV SDK for android or just give the CMakeLists.txt of my project and android studio does the rest ?
- What extra information do I need to give to who in order to build my project properly ? (as usual)
- Is there an other way to reach my goal ?
By the way this is an open source project you can check it out here:
https://bitbucket.org/BenNG/sudoku-recognizer
Thank you !

Related

Pointing Android Studio to specific cmake version

I use Android Studio 4.0.1
In my project, my local.properties has the following properties:
sdk.dir=C\:\\Users\\xxx\\AppData\\Local\\Android\\Sdk
ndk.dir=c\:\\yyy\\tools\\android-ndk-r19c
cmake.dir=c\:\\zzz\\cmake-3.17.0-win64-x64
Despite this, I get a compilation error when I Build -> Rebuild project or Clean project because my CMakeLists.txt begins with cmake_minimum_required(VERSION 3.14.3) and Android Studio uses the CMake included in the Android Studio folder instead of the one I point to in cmake.dir:
CMake Error at CMakeLists.txt:5 (cmake_minimum_required):
CMake 3.14.3 or higher is required. You are running version 3.10.2
-- Configuring incomplete, errors occurred!
See also "C:/Users/xxx/AndroidStudioProjects/E2e/app/.cxx/cmake/debug/x86/CMakeFiles/CMakeOutput.log".
FAILED: build.ninja
ninja: error: rebuilding 'build.ninja': subcommand failed
C:\Users\xxx\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\cmake.exe -HC:\Users\xxx\AndroidStudioProjects\E2e\app\src\main\cpp -BC:\Users\xxx\AndroidStudioProjects\E2e\app\.cxx\cmake\debug\x86
What am I missing for AS to pick up the cmake 3.17 I already installed when I select Rebuild Project or Clean Project?
Notes:
The directory I point to with cmake.dir is valid and being read by AS. If I introduce a typo in the path, AS complains about it.
This problem only occurs when I do Build --> Rebuild project or Clean project. If instead I do Build --> Make project, then AS will pick up the cmake version that I point to in local.properties
Clean Project worked again after deleting .cxx folder
I'm not sure about Android Studio, but you could try editing the project's build.gradle :
android {
externalNativeBuild {
cmake {
path file('CMakeLists.txt')
version '3.24.1'
}
}
}
.... and CMakeLists.txt :
cmake_minimum_required(VERSION 3.24.1)

How to build project in CLion for linux?

I'm new in CLion and CMake and try to build my project for Linux. In IDE it's work fine. But after building I try to run it with MakeFile and it do nothing. I think that problem with CMakeList.txt. Can someone check and explain how to correctly build project.
My CMakeList.txt:
cmake_minimum_required(VERSION 3.17)
project(POng2)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH "$ENV{HOME}/.local/share/JetBrains/Toolbox/apps/CLion/ch-
0/203.7717.62/bin/cmake/linux/share/cmake-3.17/Modules")
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_ttf REQUIRED)
include_directories(${SDL2_INCLUDE_DIR}
${SDL2_IMAGE_INCLUDE_DIR}
${SDL2_TTF_INCLUDE_DIR})
add_executable(POng2 main.cpp Game.h Game.cpp Paddle.h Paddle.cpp Entity.h Entity.cpp Ball.h
Ball.cpp Board.h Board.cpp Score.h Score.cpp)
target_link_libraries(POng2 ${SDL2_LIBRARY}
${SDL2_IMAGE_LIBRARIES}
${SDL2_TTF_LIBRARIES})
Files after building:
CLion uses CMake for generation build files.
For default CMake uses GodeBlock - Unix Makefile generator to create Makefile which can be built with make.
So if you want to build your app outside CLion you just need to run make in your build directory or you can use CMake to call build tool by run cmake --build . --target POng2.

How do I write the cmake script for clion to include glfw?

cmake_minimum_required(VERSION 3.15)
project(hello)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
set(CMAKE_CXX_STANDARD 11)
add_executable(hello main.cpp)
INCLUDE_DIRECTORIES(${GLFW_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(hello ${GLFW_STATIC_LIBRARIES})
It tells me
CMake Error at /home/user/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/193.5233.144/bin/cmake/linux/share/cmake-3.15/Modules/FindPkgConfig.cmake:696 (message):
None of the required 'glfw3' found
when I try to build it. My glfw folder is located at /usr/local/include/GLFW.
AFAIK, glfw3 is using CMake as the build system,
(src: packages.debian.org/fr/sid/amd64/libglfw3-dev/filelist)
which uses modern CMake, so you don't need GLFW_INCLUDE_DIRS etc...
Inside this file /usr/lib/cmake/glfw3/glfw3Targets.cmake (loaded by /usr/lib/cmake/glfw3/glfw3Config.cmake), you'll see:
...
# Create imported target glfw
add_library(glfw SHARED IMPORTED)
set_target_properties(glfw PROPERTIES
INTERFACE_COMPILE_DEFINITIONS "GLFW_DLL"
INTERFACE_INCLUDE_DIRECTORIES "/usr/include"
)
...
So you can simply use:
find_package(glfw3 REQUIRED)
...
target_link_libraries(Foo glfw)
ps: same as my previous comment

Linux opencv independent installation : not found in CMake

I am creating an object tracking program which rely on OpenCV. Thus I want to be able to test it with different versions of OpenCV but I have linking errors.
I installed the last version of OpenCV (a69b435c928f422fb5f99c02cf2dcae57dcf820a) in the following folder : /usr/local/opencv/opencv-trunk instead of the usual /usr/local.
Then I followed also the official tutorial to use OpenCV with CMake in Linux, but I had the following "normal" error :
CMake Error at CMakeLists.txt:11 (find_package):
By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "OpenCV", but CMake did not find one.
Could not find a package configuration file provided by "OpenCV" with any of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set "OpenCV_DIR" to a directory containing one of the above files. If "OpenCV" provides a separate development package or SDK, be sure it has been installed.
-- Configuring incomplete, errors occurred!
So I did what was suggested and added the following line in my CMakeLists.txt :
# Find independently installed OpenCV libraries
set(OpenCV_DIR "/usr/local/opencv/opencv-trunk/share/OpenCV")
This is the complete CMakeLists.txt file :
# Find independently installed OpenCV libraries
set(OpenCV_DIR "/usr/local/opencv/opencv-trunk/share/OpenCV")
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
add_executable( DisplayImage DisplayImage.cpp )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
Now I have the following error and I don't find how to deal with it.
CMake Error at CMakeLists.txt:12 (find_package):
Found package configuration file:
/usr/local/opencv/opencv-trunk/share/OpenCV/OpenCVConfig.cmake
but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered to be NOT FOUND.
-- Configuring incomplete, errors occurred!
If you have already faced that issue in such context your solutions are welcomed ;)
If you are running IntelliJ or any other IDE, just try re-building CMake cache (or just remove your build directory and run cmake utility again).
I believe this is not the best practice - to set the configuration variable like OpenCV_DIR hard-coded in the CMakeLists.txt. Try setting it manually, as an environment variable to the cmake utility:
OpenCV_DIR=/usr/local/Cellar/opencv3/3.1.0_2/share/OpenCV/ cmake ..
or set your IDE to make this for you:

How to include SimpleAudioEngine to Linux build in cocos2d-x v3.3 project?

I'm trying use SimpleAudioEngine in my project. I include the SimpleAudiEngine header to AppDelegate.
#include "SimpleAudioEngine.h"
When I build my project for Android everything compiles and build is fine. SimpleAudioEngine working is correct.
If run build for Linux, I get an error message:
fatal error: SimpleAudioEngine.h: No such file or directory
#include "SimpleAudioEngine.h"
I tried include SimpleAudioEngine to CMake file - result remains the previous.
I solved the problem by adding this line of code to CMakeLists.txt (in cocos2D version 3.4 project):
${COCOS2D_ROOT}/cocos/audio/include
So CMakeLists.txt is somthing like this now:
...
include_directories(
/usr/local/include/GLFW
...
${COCOS2D_ROOT}/cocos/audio/include
)
hope this help.

Resources