So, I have android project with c++ support. There are some undefined references in my project when trying to build c++ code and, after answering my first question, what is the problem with it here link, I can't find a way to do it.
If you have Cmake that looks like this:
cmake_minimum_required(VERSION 3.4.1)
# OpenCV stuff
include_directories(D:\\opencv-3.2.0-android-sdk\\OpenCV-android-sdk\\sdk\\native\\jni\\include)
add_library( lib_opencv SHARED IMPORTED )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
add_compile_options(-std=c++11)
# 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 the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
-ljnigraphics
lib_opencv
# Links the target library to the log library
# included in the NDK.
${log-lib} )
just change the part to be like this:
target_link_libraries( # Specifies the target library.
native-lib
-ljnigraphics
-landroid # Add this.
lib_opencv
# Links the target library to the log library
# included in the NDK.
${log-lib} )
Related
Hello I am beginner in Android NDK programming and I need some help getting freetype library to work with my project. I've been trying for 3 hours straight to somehow import freetype to my Android Studio project. I searched on the internet and could not find any solution that was working. I downloaded the library and put it in my cpp folder of the project.But I don't know how to include freetype. Any help would be appreaciated!
This is how my CMakeLists.txt look and for the files that I have added it works for them:
cmake_minimum_required(VERSION 3.10.2)
project("firstnative")
include_directories(stb/stb_lib
GoldFlow/Core
GoldFlow/Graphics
GoldFlow/Math
GoldFlow/glm
GoldFlow/glm/gtc
GoldFlow/entt
GoldFlow/physics
GoldFlow/scripts
GoldFlow/freetype/include
GoldFlow/freetype/include/freetype/
GoldFlow/freetype/include/freetype/config
GoldFlow/freetype/include/freetype/internal
GoldFlow/freetype/include/freetype/internal/services
)
add_library(
Native
SHARED
GoldFlow/Math/GoldMath.cpp
GoldFlow/Graphics/Shader.cpp
GoldFlow/Graphics/Renderer2D.cpp
GoldFlow/Graphics/Camera.cpp
GoldFlow/Graphics/Texture.cpp
GoldFlow/Graphics/SpriteSheet.cpp
GoldFlow/Core/Scene.cpp
GoldFlow/Core/GameObject.cpp
GoldFlow/Core/Application.cpp
GoldFlow/Core/Controls.cpp
GoldFlow/Core/Timer.cpp
GoldFlow/physics/AABB.cpp
GoldFlow/physics/Objects.cpp
GoldFlow/scripts/ControllerScript.cpp
GoldFlow/scripts/CharacterMovingScript.cpp
native.cpp)
add_library(
Freetype
SHARED
GoldFlow/freetype/src/autofit/autofit.c
GoldFlow/freetype/src/base/ftbase.c
GoldFlow/freetype/src/base/ftbbox.c
GoldFlow/freetype/src/base/ftbdf.c
GoldFlow/freetype/src/base/ftbitmap.c
GoldFlow/freetype/src/base/ftcid.c
GoldFlow/freetype/src/base/ftfstype.c
GoldFlow/freetype/src/base/ftgasp.c
GoldFlow/freetype/src/base/ftglyph.c
GoldFlow/freetype/src/base/ftgxval.c
GoldFlow/freetype/src/base/ftinit.c
GoldFlow/freetype/src/base/ftmm.c
GoldFlow/freetype/src/base/ftotval.c
GoldFlow/freetype/src/base/ftpatent.c
GoldFlow/freetype/src/base/ftpfr.c
GoldFlow/freetype/src/base/ftstroke.c
GoldFlow/freetype/src/base/ftsynth.c
GoldFlow/freetype/src/base/fttype1.c
GoldFlow/freetype/src/base/ftwinfnt.c
GoldFlow/freetype/src/bdf/bdf.c
GoldFlow/freetype/src/bzip2/ftbzip2.c
GoldFlow/freetype/src/cache/ftcache.c
GoldFlow/freetype/src/cff/cff.c
GoldFlow/freetype/src/cid/type1cid.c
GoldFlow/freetype/src/gzip/ftgzip.c
GoldFlow/freetype/src/lzw/ftlzw.c
GoldFlow/freetype/src/pcf/pcf.c
GoldFlow/freetype/src/pfr/pfr.c
GoldFlow/freetype/src/psaux/psaux.c
GoldFlow/freetype/src/pshinter/pshinter.c
GoldFlow/freetype/src/psnames/psnames.c
GoldFlow/freetype/src/raster/raster.c
GoldFlow/freetype/src/sfnt/sfnt.c
GoldFlow/freetype/src/smooth/smooth.c
GoldFlow/freetype/src/truetype/truetype.c
GoldFlow/freetype/src/type1/type1.c
GoldFlow/freetype/src/type42/type42.c
GoldFlow/freetype/src/winfonts/winfnt.c
)
find_library(
log-lib
log )
find_library(GLES-lib
GLESv3)
target_link_libraries(
Native
${log-lib}
${GLES-lib}
${Freetype}
)
The error I am getting now is this: C:\Users\infer\AndroidStudioProjects\FirstNative\app\src\main\cpp\GoldFlow\freetype\src\base\ftbdf.c:40:14: error: use of undeclared identifier 'FT_ERR_PREFIXInvalid_Face_Handle'; did you mean 'FT_Err_Invalid_Face_Handle'?
Ok the solution was very simple. All I did actually was I created directory in cpp folder named freetype and in that dir I've put every freetype file and just added that folder as sub directory in CMake and linked at the end and now eveyrthing works. Here is my CMake:
# 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.10.2)
# Declares and names the project.
project("firstnative")
include_directories(stb/stb_lib
GoldFlow/Core
GoldFlow/Graphics
GoldFlow/Math
GoldFlow/glm
GoldFlow/glm/gtc
GoldFlow/entt
GoldFlow/physics
GoldFlow/scripts
GoldFlow/text
)
# 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
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
GoldFlow/Math/GoldMath.cpp
GoldFlow/Graphics/Shader.cpp
GoldFlow/Graphics/Renderer2D.cpp
GoldFlow/Graphics/Camera.cpp
GoldFlow/Graphics/Texture.cpp
GoldFlow/Graphics/SpriteSheet.cpp
GoldFlow/Core/Scene.cpp
GoldFlow/Core/GameObject.cpp
GoldFlow/Core/Application.cpp
GoldFlow/Core/Controls.cpp
GoldFlow/Core/Timer.cpp
GoldFlow/physics/AABB.cpp
GoldFlow/physics/Objects.cpp
GoldFlow/scripts/ControllerScript.cpp
GoldFlow/scripts/CharacterMovingScript.cpp
GoldFlow/text/TextRenderer.cpp
native.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.
#sd
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 )
find_library(GLES-lib
GLESv3)
add_subdirectory(freetype)
target_link_libraries( # Specifies the target library.
Native
# Links the target library to the log and gl es library
# included in the NDK.
${log-lib}
${GLES-lib}
freetype
)
I eventually cross-compiled a capstone library for android, but now I am having trouble linking it with my main .so library(native-lib).
I followed the steps shown in the official web site.
My CMakeList.txt is as below.
# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.
cmake_minimum_required(VERSION 3.4.1)
# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.
add_library( # Specifies 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 )
find_library( # Defines the name of the path variable that stores the
# location of the NDK library.
log-lib
# Specifies the name of the NDK library that
# CMake needs to locate.
log )
# Links your native library against one or more other native libraries.
#target_link_libraries( # Specifies the target library.
# native-lib
#
# # Links the log library to the target library.
# ${log-lib} )
add_library( capstone
SHARED
IMPORTED )
set_target_properties( # Specifies the target library.
capstone
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.
C:/Users/82102/AndroidStudioProjects/Android-Disassembler/capstone/${ANDROID_ABI}/libcapstone.so.5 )
target_link_libraries( native-lib capstone ${log-lib} ) #app-glue
include_directories( capstone/include/ )
The APK built has native-lib.so, but does not have 'libcapstone.soorlibcapstone.so.5`. So when I launch the APK, the error below happens.
2019-08-22 14:21:01.340 6122-6122/com.kyhsgeekcode.disassembler E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kyhsgeekcode.disassembler, PID: 6122
java.lang.UnsatisfiedLinkError: dlopen failed: library "libcapstone.so.5" not found
at java.lang.Runtime.loadLibrary0(Runtime.java:1016)
at java.lang.System.loadLibrary(System.java:1669)
at com.kyhsgeekcode.disassembler.MainActivity.<clinit>(MainActivity.java:169)
at java.lang.Class.newInstance(Native Method)
I tried adding a jniLibs directory in the folder and adding the libcapstone.so.5, but the APK built still does not contain libcapstone.so.(5) and the same error happens.
How should I edit my CMakeList.txt to correctly link a custom prebuilt library to a main shared library?(native-lib.so)
I changed my CMakeLists.txt to link the capstone as a static library.
# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.
cmake_minimum_required(VERSION 3.4.1)
# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.
add_library( # Specifies 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 )
find_library( # Defines the name of the path variable that stores the
# location of the NDK library.
log-lib
# Specifies the name of the NDK library that
# CMake needs to locate.
log )
# Links your native library against one or more other native libraries.
#target_link_libraries( # Specifies the target library.
# native-lib
#
# # Links the log library to the target library.
# ${log-lib} )
#add_library( capstone
# SHARED
# IMPORTED )
#set_target_properties( # Specifies the target library.
# capstone
#
# # Specifies the parameter you want to define.
# PROPERTIES IMPORTED_LOCATION#
#
# # Provides the path to the library you want to import.
# C:/Users/82102/AndroidStudioProjects/Android-Disassembler/capstone/${ANDROID_ABI}/libcapstone. )
target_link_libraries( native-lib C:/Users/82102/AndroidStudioProjects/Android-Disassembler/capstone/${ANDROID_ABI}/libcapstone.a ${log-lib} ) #app-glue
include_directories( capstone/include/ )
Hopefully, it worked!
I have a static library built for arm64-v8a architecture. I need to write a wrapper to call one of the main function of the static library and pass the result to java code. For this I have create a new native project in android studio and added the static library in cpp folder as mylib.a.
I have tried to add this library in CMakeLists.txt but the project is not compiling. Below is my CMakeLists.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).
native-lib.cpp)
add_library( # Sets the name of the library.
mylib
# Sets the library as a shared library.
STATIC
# Provides a relative path to your source file(s).
mylib.a)
# 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}
${mylib})
I am newbie to CMake. Please help me to link the static library with my shared library.
CMake accesses prebuilt libraries via an imported library: https://cmake.org/cmake/help/latest/command/add_library.html#imported-libraries
add_library(mylib
STATIC
IMPORTED
mylib.a)
I am creating a demo to read and write files through android NDK. I am using CMake and have CMakeLists.txt in my project with following content:
# 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 have created a new utility.cpp and utility.h file in the same folder where native-lib.cpp saved. I am able to include utility.h in native-lib.cpp. When I run the program I got an error undefined reference to function {fun} where {fun} is a function written in utility.cpp.
Please suggest how can I build both the source files in my project.
Just add the cpp to your CMakeLists
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
src/main/cpp/utility.cpp
)
Replace
src/main/cpp/native-lib.cpp)
with
src/main/cpp/utility.cpp
src/main/cpp/native-lib.cpp)
if utility is in src/main/cpp/
This question already has answers here:
Automatically add all files in a folder to a target using CMake?
(5 answers)
Closed 5 years ago.
In my project I am using cpp and .h files they all are in different folders
|-src
|-main
|-java
|-cpp
|-native-lib.cpp
|-library-1
|-include
|-lib11.h
|-lib12.h
|-...
|-library-2
|-include
|-lib21.h
|-lib22.h
|-...
in current version i am adding each files into 'CMakeLists.txt'
...
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
src/main/cpp/library-1/include/lib10.h
src/main/cpp/library-1/include/lib11.h
src/main/cpp/library-1/include/lib12.h
src/main/cpp/library-1/include/lib13.h
...
src/main/cpp/library-2/include/lib21.h
src/main/cpp/library-2/include/lib22.h
src/main/cpp/library-2/include/lib23.h
...
)
...
I tried adding like below
...
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
src/main/cpp/library-1/include/*.h #expected result: it will include all '.h' files in directory, but gradle sync failed
src/main/cpp/library-2/include/*.h#expected result: it will include all '.h' files in directory, but gradle sync failed
)
...
Any other way to include all files in a folder using add_library
after suggestion from Lucky
# 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.
include_directories(src/main/cpp/library-1/include)
include_directories(src/main/cpp/library-2/include)
file(GLOB SOURCES "src/*.h")
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
${SOURCES}
)
# 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} )
You can generate a list of your source files:
file(GLOB SOURCES "src/*.h")
Now the SOURCES variable can be used as a parameter in add_library.
add_library(native-lib SHARED ${SOURCES})