I am trying to build an android application that uses static libraries from some existing c++ code. However I cannot seem to get things building, here are the steps I have taken so far..
I have ndk-r5b and have built the standalone toolchain as per ndk/docs/STANDALINE-TOOLCHAIN.html. I have then used the standalone toolchain compiler (arm-linux-androideabi-g++) instead of g++ for the CXX flag in the Makefile that compiles the static libraries I need. This compiles without errors and there are 3 static libraries produced.
Here is a code snippet of some of the flags used to build the prebuilt libraries:
CXX = arm-linux-androideabi-g++
SYSTEM_LIBS = -lstdc++ -lm
INCLUDE_PATH += ${NDK_PATH}/platforms/android-8/arch-arm/usr/include/
Here is a sample line that is produced from the makefile when compiling:
arm-linux-androideabi-g++ -c -DTIME_SIM -I./include -I/home/greg/dev/Android/android-ndk-r5b/platforms/android-8/arch-arm/usr/include/ -fpic -ggdb3 -SimTime.C -o SimTime.o
Next I build the app using ndk-build using the following for Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := engine
LOCAL_SRC_FILES := ../libs/libEngine.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := shmem
LOCAL_SRC_FILES := ../libs/libShMem.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := util
LOCAL_SRC_FILES := ../libs/libUtil.a
include $(PREBUILT_STATIC_LIBRARY)
# build server as a shared library
include $(CLEAR_VARS)
LOCAL_MODULE := libServer
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../include
LOCAL_SRC_FILES := \
Server.C \
Router.C \
RouterMsgs.C \
Federation.C \
cripName.C \
ver.C \
JNIWrapper.cpp
LOCAL_STATIC_LIBRARIES := engine shmem util
include $(BUILD_SHARED_LIBRARY)
The prebuilt libraries compile fine using the standalone toolchain compiler given in the android ndk. However there are many unresolved references to ostream when linking the shared library to the prebuilt libraries using ndk-build. For exampe:
/home/android/obj/local/armeabi/libShMem.a(SubscriptionItem.o): In function `SUBSCRIPTION_ITEM::Print(std::basic_ostream<char, std::char_traits<char> >&)':/home/src/comm/SubscriptionItem.C:97: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
I assume I am missing some important flags or not doing something correct when I am compiling using the standalone compiler but any help or insight on this issue would be greatly appreciated as I cant seem to find this answer on google or in any of the android ndk docs. Thanks!
Well, you can actually fix that with creating a Application.mk file inside the same folder as the Android.mk file is, containing:
APP_STL := stlport_static
for using the static stlport that is located inside the Android NDK.
I had the same issue and resolved it by adding a module for the standard C++ library. The library linked by the ndk-build system is from the wrong location (platforms/android-9/arch-arm/usr/lib in my case).
include $(CLEAR_VARS)
LOCAL_MODULE := rightstdc
LOCAL_SRC_FILES := <path to the correct libstdc++.a>
include $(PREBUILT_STATIC_LIBRARY)
Add the module tag to the list of static libraries:
LOCAL_STATIC_LIBRARIES := engine shmem util rightstdc
The build/core/build-binary.mk prepends -L$(SYSROOT)/usr/lib if any libraries are specified in LOCAL_LDLIBS but in my case that is the wrong path.
I don't know if there is a missing step that should copy the correct libstdc++ to that location but the approach above will work.
Related
I'm trying to compile my application to use tensorflow C++ library after building lintensorflow_core.a into my NDK application. The application has the main java layer, that communicates with the NDK component. In that NDK component, I'm looking to call the tensorflow as such: using namespace ::tensorflow::ops; // NOLINT(build/namespaces)
I've seen bunch of links on how to do it with Android.mk file. But apparently, that does not gets created anymore in the new version of android studio. If it is possible with just Android.mk and ndk-build command, that would be great as well. However, the combination of Android.mk and ndk-build has also failed me. The output basically nothing.
I did have plenty of variation of Android.mk file and here's one of them:
LOCAL_PATH := $(call my-dir)
TENSORFLOW_HOME := $(LOCAL_PATH)/../../../../../tensorflow
TENSORFLOW_CORE := $(LOCAL_PATH)/../../../../../tensorflow/tensorflow/core
TENSORFLOW_OPS := $(LOCAL_PATH)/../../../../../tensorflow/tensorflow/core/ops
include $(CLEAR_VARS)
LOCAL_MODULE := tensorflow
LOCAL_SRC_FILES := $(TENSORFLOW_HOME)/tensorflow/contrib/makefile/gen/lib/android_armeabi-v7a/libtensorflow-core.a
LOCAL_LDLIBS := -static -Wl,--build-id -Wl,--allow-multiple-definition -Wl,--whole-archive
LOCAL_CFLAGS := -std=c++11 -I$(TENSORFLOW_HOME)
LOCAL_C_INCLUDES := $(TENSORFLOW_OPS)
TARGET_ARCH_ABI := armeabi
include $(PREBUILT_STATIC_LIBRARY)
But this has been a total failure. Could someone point me to the right direction?
I have to use some c++ code in my android application. This code was used successfully in an iOS project.
The code depends on 2 external libraries: zero-mq and protocol buffers.
I compiled the zmq library as an static library like explained here. I added the static (.a) library and the .jar to my project.
I created the protobuf library with the following configurations:
./configure --host=arm-eabi --with-sysroot=x/android-ndk-r10d/platforms/android-21/arch-arm CC="x/android-ndk-r10d/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc --sysroot x/android-ndk-r10d/platforms/android-21/arch-arm" --enable-cross-compile --with-protoc=protoc LIBS=-lc
make
I changed the real directories to x to make them shorter.
In my Android Project(IDE: Android Studio) I prepared everything which is necessary. I created a JNI Folder and deactivated the auto-creation of the makefiles.
Application.mk:
APP_MODULE := proxy
APP_STL := gnustl_shared
APP_CPPFLAGS := -frtti -fexceptions --std=c++11
APP_ABI := armeabi-v7a ##all later
NDK_TOOLCHAIN_VERSION := 4.9
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := zmq_static
LOCAL_SRC_FILES := zmq/libzmq.a
include $(PREBUILD_STATIC_LIBRARY)
LOCAL_MODULE := protobuf_static1
LOCAL_SRC_FILES := protobuf/libprotobuf.a
LOCAL_EXPORT_C_INCLUDES := google/protobuf protobuf/
include $(PREBUILD_STATIC_LIBRARY)
LOCAL_MODULE := protobuf_static2
LOCAL_SRC_FILES := protobuf/libprotobuf-lite.a
LOCAL_EXPORT_C_INCLUDES := google/protobuf protobuf/
include $(PREBUILD_STATIC_LIBRARY)
LOCAL_MODULE := protobuf_static3
LOCAL_SRC_FILES := protobuf/libprotoc.a
LOCAL_EXPORT_C_INCLUDES := google/protobuf protobuf/
include $(PREBUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := proxy
LOCAL_CFLAGS := -I/include -pthread -lpthread -D__GXX_EXPERIMENTAL_CXX0X__ - frtti
LOCAL_CPPFLAGS := -I/include -pthread -lpthread -D__GXX_EXPERIMENTAL_CXX0X__ -frtti
LOCAL_CPP_FEATURES += exceptions
LOCAL_LDLIBS := -llog
LOCAL_SRC_FILES := \
usersession.cpp\
## LOCAL_ALLOW_UNDEFINED_SYMBOLS := true will compile the code but shutdown on runtime
LOCAL_C_INCLUDES += C:\Users\M\Dropbox\Workspace\ndk_swig_test\app\src\main\jni
LOCAL_C_INCLUDES += C:\Users\M\Dropbox\Workspace\ndk_swig_test\app\src\arm\jni
LOCAL_C_INCLUDES += C:\Users\M\Dropbox\Workspace\ndk_swig_test\app\src\debug\jni
LOCAL_C_INCLUDES += C:\Users\M\Dropbox\Workspace\ndk_swig_test\app\src\armDebug\jni
LOCAL_C_INCLUDES += \zmq
LOCAL_C_INCLUDES += \protobuf
LOCAL_STATIC_LIBRARIES := zmq_static protobuf_static1 protobuf_static2 protobuf_static3
LOCAL_WHOLE_STATIC_LIBRARIES := zmq_static protobuf_static1 protobuf_static2 protobuf_static3
include $(BUILD_SHARED_LIBRARY)
The zmq library is in the subdirectory zmq and the protobuf library is in the subfolder protobuf.
Now the linking of the Objects still does not work. The Error Output when I execute ndk-build:
C:\Users\M\Dropbox\Workspace\ndk_swig_test\app\src\main\jni>ndk-build
[armeabi-v7a] SharedLibrary : libproxy.so
C:/Users/M/Documents/ndk/sources/cxx-stl/gnu- libstdc++/4.9/include/ext/new_allocator.h:127: error: undefined reference to 'ControlledInstance::ControlledInstan (std::shared_ptr<protogen::Application>, std:
:shared_ptr<protogen::Role>, std::shared_ptr<protogen::User>)'
C:/Users/M/Documents/ndk/sources/cxx-stl/gnu- libstdc++/4.9/include/bits/shared_ptr_base.h:511: error: undefined reference to 'protogen::User::User()'
C:/Users/M/Documents/ndk/sources/cxx-stl/gnu- libstdc++/4.9/include/bits/shared_ptr_base.h:914: error: undefined reference to 'google::protobuf::internal::empty tring_'
C:/Users/M/Dropbox/Workspace/ndk_swig_test/app/src/main//jni/controlledinstance.h :23: error: undefined reference to 'protogen::MetaGraph::~MetaGraph()'
collect2.exe: error: ld returned 1 exit status
make.exe: *** [C:/Users/M/Dropbox/Workspace/ndk_swig_test/app/src/main//obj/local/armeabi- v7a/libproxy.so] Error 1
I tried many versions of the Android.mk and recreated the library more than once with different options which I found all over the internet.
I also looked at dozens of threads on stackoverflow which did not help me.(I'm not allowed to link them because of low reputation)
Additionally i read most of the doc files from the ndk e.g. PREBUILTS.
I added some other directories to my JNI directory e.g. the directory with the original files and directories (compiler, io, stubs...). I think this directory should offer the export of the necessary methods if the prebuild library was successfully linked to my shared library - which is not the case.
I tried far more than I can explain in few minutes and I think it would be overkill if i added everything I've tried because nothing helped.
Because this is my first question I dont have the reputation to include more than 2 links. Sorry for that.
There may probably be other issues as well, but you at least have got a typo - it should be include $(PREBUILT_STATIC_LIBRARY), as in, BUILT, not BUILD.
I was given a shared library built on Linux x86, let's call it libA.so, and I want to use the function calls provided by this library SDK.
I am having issues building and have a few questions:
1) I will be able to build for x86, but will I be able to build for arm? I believe the answer is no, meaning I cannot run on a Nexus 5 for example.
2) The ndk-build complains of the #include that should be resolved by my LOCAL_SHARED_LIBRARIES. I am not sure why that is. My Android.mk is as follow:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := B
LOCAL_SRC_FILES := B.cpp
LOCAL_SHARED_LIBRARIES := A
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
Can someone please help me resolve my Android.mk? I don't understand why it is complaining about my include statement in B.cpp. Please let me know if I can run B in an arm environment, although the SDK I am relying on was built on x86.
If your shared library libA.so has been compiled for linux-x86, it will certainly not run on android x86 targets (mainly because it needs to be linked to Bionic C library instead of glibc), and absolutely not on android arm devices.
Then, to solve your second issue, if you can get properly compiled android shared libraries for your android targets, you would include your library this way:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := A
LOCAL_SRC_FILES := ../libA/prebuilts/$(TARGET_ARCH_ABI)/libA.so # path to libA .so file, depending on the target ABI.
LOCAL_EXPORT_C_INCLUDES := ../libA/includes # path to libA headers.
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := B
LOCAL_SRC_FILES := B.cpp
LOCAL_SHARED_LIBRARIES := A
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
I'm beginer to Android NDK. I want to build a RSA example base on openssl libary.
First, I built libssl.so and libcrypto.so librairies with ndk-build in the guardianproject.
Next, I create a new sample project to integrate libary (libss.so & lybcryto.so). I follow the same in this post
My App directory
TrialApp
|
|-->Activity.java (includes System.LoadLibrary calls)
|
|-->jni
|-->TestJNI2.cpp
|
|-->Android.mk
|
|-->includes
| |
| |-->openssl (dir containing *.h files)
|
|-->precompiled
|-->libssl.so
|-->libcrypto.so
My android.mk:
LOCAL_PATH := $(call my-dir)
# Prebuilt libssl
include $(CLEAR_VARS)
LOCAL_MODULE := ssl
LOCAL_SRC_FILES := precompiled/libssl.so
include $(PREBUILT_SHARED_LIBRARY)
# Prebuilt libcrypto
include $(CLEAR_VARS)
LOCAL_MODULE := crypto
LOCAL_SRC_FILES := precompiled/libcrypto.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
c_includes := $(LOCAL_PATH)
cf_includes := includes/openssl
cf_includes := $(addprefix -Ijni/,$(cf_includes))
export_c_includes := $(c_includes)
LOCAL_MODULE := security
LOCAL_SRC_FILES := TestJNI2.cpp
LOCAL_CFLAGS += $(cf_includes)
LOCAL_EXPORT_C_INCLUDES := $(export_c_includes)
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS += $(LOCAL_PATH)/precompiled/libssl.so
LOCAL_LDLIBS += $(LOCAL_PATH)/precompiled/libcrypto.so
include $(BUILD_SHARED_LIBRARY)
TestJNI2.cpp
/* OpenSSL headers */
#include "openssl/bio.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
/* Initializing OpenSSL */
void init_openssl(void){
SSL_load_error_strings();
ERR_load_BIO_strings();
OpenSSL_add_all_algorithms();
}
Then when i build with ndk-build always have problems like this
/data/workspace/TestJNI2/jni/TestJNI2.cpp:3:25: error: openssl/bio.h: No such file or directory
/data/workspace/TestJNI2/jni/TestJNI2.cpp:4:25: error: openssl/ssl.h: No such file or directory
/data/workspace/TestJNI2/jni/TestJNI2.cpp:5:25: error: openssl/err.h: No such file or directory
/data/workspace/TestJNI2/jni/TestJNI2.cpp: In function 'void init_openssl()':
/data/workspace/TestJNI2/jni/TestJNI2.cpp:10: error: 'SSL_load_error_strings' was not declared in this scope
/data/workspace/TestJNI2/jni/TestJNI2.cpp:11: error: 'ERR_load_BIO_strings' was not declared in this scope
/data/workspace/TestJNI2/jni/TestJNI2.cpp:12: error: 'OpenSSL_add_all_algorithms' was not declared in this scope
make: *** [/data/workspace/TestJNI2/obj/local/armeabi/objs/security/TestJNI2.o] Error 1
Can anyone help me? Or how to build an example RSA algorthim base on openssl lib?
You should build static libraries for libssl and libcrypto in your script. If you can't, rename these libraries (you can do this after build, while copying to your precompiled directory). The reason is that the system comes with its own (probably different) version of these shared libraries, and the loader will use /system/lib/libssl.so and /system/lib/libcrypto.so instead of your private copies.
Regarding the Android.mk file, I slightly cleaned it up for you (note that I did not change the names of prebuilt LOCAL_MODULEs, but changed the name of LOCAL_MODULE you finally build, because security is, well, too generic and could also happen to match a system library on some device):
LOCAL_PATH := $(call my-dir)
# Prebuilt libssl
include $(CLEAR_VARS)
LOCAL_MODULE := ssl
LOCAL_SRC_FILES := precompiled/libPrivateSsl.so
include $(PREBUILT_SHARED_LIBRARY)
# Prebuilt libcrypto
include $(CLEAR_VARS)
LOCAL_MODULE := crypto
LOCAL_SRC_FILES := precompiled/libPrivateCrypto.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := PrivateSecurity
LOCAL_C_INCLUDES := includes
LOCAL_SRC_FILES := TestJNI2.cpp
LOCAL_LDLIBS := -llog
LOCAL_SHARED_LIBRARIES := ssl crypto
include $(BUILD_SHARED_LIBRARY)
Don't forget that your Java should load the dependencies first:
{
System.loadLibrary("PrivateSsl");
System.loadLibrary("PrivateCrypto");
System.loadLibrary("PrivateSecurity");
}
I'm beginer to Android NDK. I want to build a RSA example base on openssl libary.
You can, but you have to be careful. Here's the reason Alex told you to use static libraries for libssl and libcrypto in your script.
The master Android process is zygote. Its like init in Linux. Zygote loads OpenSSL when its start, and it loads version 0.9.8. If you link against OpenSSL 1.0.1, then you will get mysterious runtime crashes. The crashes are due to the Android loader using the 0.9.8 version of the library (already mapped from Zygote), and not your version of OpenSSL.
You can use a shared object, but your shared object must be a wrapper around the static version of libssl and libcrypto.
If you are not using Android's build system, then you can find additional instructions for building purely from the command line at OpenSSL's wiki. The wiki page includes setting the envrionment and cross compiling. See FIPS Library and Android.
Another thing to watch out for: be sure to build with -mfloat-abi=softfp. The stock OpenSSL misses that when cross compiling. You need it to ensure floats are passed on the stack, and not through floating point registers. Otherwise, all your floats will mysteriously have 0.0f value (like the floats used to estimate entropy in RAND_add).
Case:
I am building an app which uses libcrypto and libssl. I am trying to use prebuilt libcrypto.so and libssl.so and compile my application.
But I keep getting undefined reference errors.
My App/Android.mk
LOCAL_PATH := $(call my-dir)
my_LOCAL_PATH := $(LOCAL_PATH)
include $(call all-subdir-makefiles)
LOCAL_PATH := $(my_LOCAL_PATH)
common_SRC_FILES := \
src/foo.c
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(common_SRC_FILES)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../openssl/include
LOCAL_SHARED_LIBRARIES += mylibssl mylibcrypto
include $(BUILD_STATIC_LIBRARY)
My App/mylibssl/Android.mk
Building mylibssl [from a prebuilt libssl.so]
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mylibssl
LOCAL_SRC_FILES := libssl.so
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_MODULE_SUFFIX := .so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../openssl/include
include $(BUILD_PREBUILT)
My App/mylibcrypto/Android.mk
Building mylibcrypto [from a prebuilt libcrypto.so]
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mylibcrypto
LOCAL_SRC_FILES := libcrypto.so
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_MODULE_SUFFIX := .so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../openssl/include
include $(BUILD_PREBUILT)
I keep getting
error: undefined reference to 'BIO_new_mem_buf'
error: undefined reference to 'PEM_read_bio_X509_AUX'
error: undefined reference to 'BIO_free'
...
I have spent several hours trying to figure out and am totally stuck. Please help!
I am building an app which uses libcrypto and libssl... But I keep getting undefined reference errors.
error: undefined reference to 'BIO_new_mem_buf'
error: undefined reference to 'PEM_read_bio_X509_AUX'
error: undefined reference to 'BIO_free'
These are linker errors, and mean you are not linking against the OpenSSL library. Its probably a path problem, assuming you have an Android version of the OpenSSL library available.
If you need an an Android version of the OpenSSL library, then you can build it yourself or find it on Github. To build it yourself, see FIPS Library and Android (just ignore the FIPS stuff). The steps required are (and note the leading dot "." when running the script):
cd openssl-1.0.1f/
wget http://wiki.openssl.org/images/7/70/Setenv-android.sh
chmod a+x *.sh
. ./setenv-android.sh
./config shared no-ssl2 no-hw no-engine --openssldir=/usr/local/ssl/android-18/ <other options>
make depend
make all
For option two, you can search Github with https://www.google.com/q=openssl+android+site:github.com.
Building mylibcrypto [from a prebuilt libcrypto.so]
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mylibcrypto
LOCAL_SRC_FILES := libcrypto.so
This is not going to produce expected results, and will likely result in a crash if you get it to work. The problem is Android uses 0.9.8, and you are probably building against 1.0.1.
What happens is Zygote loads OpenSSL 0.9.8 at startup (its the parent of all Android Java programs). When your app is launched, Zygote is forked so OpenSSL is already mapped into the address space. That means the version of OpenSSL you are carrying around is not loaded. Later, you crash because 0.9.8 and 1.0.1 are not binary compatible (i.e., they are ABI incompatible).
You are correct in building a wrapper shared object (mylibcrypto.so). However, mylibcrypto.so will need to statically link against OpenSSL to avoid the above problem. That is, mylibcrypto.so will need to link against libcrypto.a and libssl.a.