I'm trying to build a wraper using some old C++ code in Android.
When compiling the errors bellow are shown:
In file included from /usr/local/android/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/bits/stl_algobase.h:61:0,
from /usr/local/android/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/bits/stl_tree.h:63,
from /usr/local/android/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/map:60,
from /home/vocalize/source/xxxxxxxxxxxxxxxxxxxxx/Lxxxxxxx.h:9,
from /home/vocalize/source/xxxxxxxxxxxxxxxxx/jni/cxxx_wrap.c:3:
/usr/local/android/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/bits/functexcept.h:43:1: error: unknown type name 'namespace'
/usr/local/android/android-ndk-r8b/sources/cxx-stl/gnu-libstdc++/4.6/include/bits/functexcept.h:44:1: error: expected ',' or ';' before '{' token
I'm using the following Makefile.mk
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(MY_LIB_DIR)include
LOCAL_CFLAGS += -DHAVE_CONFIG_H
LOCAL_CFLAGS += -DANDROID_NDK
LOCAL_PATH := $(BASE_PATH)
LOCAL_MODULE := cxxxx_lib
LOCAL_SRC_FILES := cxxxx_wrap.c
LOCAL_STATIC_LIBRARIES := my_lib
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
What I can do to fix these errors?
A C++ .h file is being included from cxxx_wrap.c, which is a .c file. The compiler uses the extension of the source file to detect the language. So it's assuming C, and choking on the C++-specific syntax.
Rename cxxx_wrap.c to .cpp or .cxx. Or surround the #include "Lxxxxxxx.h" line with #ifdef __cplusplus/#endif. Or force C++ compilation by specifying -x c++ compiler option.
Once you do, make sure all JNI methods in cxxx_wrap are declared with JNIEXPORT or surrounded with extern "C" {}. Otherwise, the Java run-time won't find them.
For the record: renaming the .h file to .hpp won't help.
Related
My source file can not include some header, because of local flag is not defined.
SSS.cpp:
#include <jni.h>
//This code is not defined:
#ifdef WORD
#include "Word.h"
#endif
//...rest of code
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := word
LOCAL_CPPFLAGS := -DWORD
LOCAL_SRC_FILES := SSS.cpp
include $(BUILD_SHARED_LIBRARY)
And one more moment, project build was successful, but I can not run it because of lots of errors in source file (Eclipse c++ editor still can not see my header).
Probably not -WORD but -DWORD ?
-D defines a macro to be used by the preprocessor.
And what this
-std=c++11
belongs to?
I want to use the RSA of openssl, so I need integrate openssl to Android with ndk.I download the source of openssl for Android at https://github.com/aluvalasuman/OpenSSL1.0.1cForAndroid.
ndk-build in the source folder generated libcrypto.so and libssl.so, I copied them to $(MY_PROJECT)/jni/lib/, then linked them in Android.mk like that:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_LIBS := $(LOCAL_PATH)/lib
LOCAL_STATIC_LIBRARIES := libcrypto libssl
LOCAL_LDLIBS := -llog
LOCAL_MODULE := jni
LOCAL_SRC_FILES := jni.c
include $(BUILD_SHARED_LIBRARY)
And tested the openssl like that:
#include <stdio.h>
#include <string.h>
#include <openssl/rsa.h>
#define nr_bits 2048
int test_openssl()
{
RSA *rsa = RSA_generate_key(nr_bits, 65537, NULL, NULL);
return 0;
}
When I compile it , it threw the error:
....jni/license/license.c:10: error: undefined reference to 'RSA_generate_key'
collect2: ld returned 1 exit status
Does anyone know what's the problem? I will be very grateful if anyone could help.
Such undefined reference errors typically indicate that the compiler/linker did not find libcrypto.so in it's path. You could try a couple of things:
Add -lcrypto to LOCAL_LDLIBS. This tells the compiler to use libcrypto.so, if possible, to resolve undefined symbols
Add $(MY_PROJECT)/jni/lib/ to LOCAL_LIBS so the compiler knows where to pick -lcrypto from. This assumes that $(MY_PROJECT) is defined in the make file.
I know this is an old question, but if someone is experiencing this problem, she or he could solve the problem by using:
LOCAL_STATIC_LIBRARIES := crypto
instead of
LOCAL_STATIC_LIBRARIES := libcrypto libssl
So I'm attempting to use libopus on my native code for an Android application.
My Android.mk file looks like this:
PLATFORM_PREFIX := /opt/android-ext/
LOCAL_PATH := $(PLATFORM_PREFIX)/lib
include $(CLEAR_VARS)
LOCAL_MODULE := libopus
LOCAL_SRC_FILES := libopus.a
include $(PREBUILT_STATIC_LIBRARY)
# I have to redeclare LOCAL_PATH because the library is in /opt/android-ext/
# and my project is somewhere else. Not very elegant.
LOCAL_PATH := /home/sergio/workspace/Project/jni
include $(CLEAR_VARS)
LOCAL_MODULE := opusUtilsNative
LOCAL_SRC_FILES := opusUtilsNative.c
LOCAL_C_INCLUDES += $(PLATFORM_PREFIX)/include
LOCAL_STATIC_LIBRARIES := android_native_app_glue libopus
include $(BUILD_SHARED_LIBRARY)
And my code in opusUtilsNative.c looks like this:
#include "opusUtilsNative.h"
#include <opus/opus.h>
#include <opus/opus_types.h>
JNIEXPORT jbyteArray JNICALL Java_Project_OpusUtils_encode
(JNIEnv * je, jclass jc, jbyteArray data){
int rc;
opus_int16 * testOutBuffer;
unsigned char* opusBuffer;
OpusDecoder *dec;
dec = opus_decoder_create(48000, 2, &rc);
return data;
}
And when I try to build it, it works fine only if I remove the line that uses the "opus_decoder_create" function. Else I will get this:
error: undefined reference to 'opus_decoder_create'
I can see that opus_decoder_create is clearly defined on opus.h, which is clearly being included since if I exclude that line, I'll get an error regarding the opus_int16 and OpusDecoder declarations. How come some definitions are being included and some aren't?
Any help will be greatly appreciated.
This was tricky. After digging around for a bit, I realized I hadn't cross-compiled the opus library correctly, and I didn't have an ARM binary after all.
A good way to verify if your library was cross-compiled correctly:
cd /opt/android-ext/lib #Or wherever the .a file is
ar x libopus.a
file tables_LTP.o #Or any of the .o files generated by ar x
The output should look like this:
tables_LTP.o: ELF 32-bit LSB relocatable, ARM, version 1 (SYSV), not stripped
Otherwise, you might want to double-check your cross-compilation process.
It's error from linker, not from compiler. You forgot to add reference to correspondent libraries to your Android.mk file, do smth like this:
LOCAL_LDLIBS += -lopus
I forgot to integrate one key library
LOCAL_LDLIBS := -lGLESv2
This fixed my problem.
I have the latest android ndk installed. I am using Eclipse along with Sequoyah.
I am trying to use various things that should be found in stlport or gnustl libraries, but I keep getting errors that they cannot be found.
In JNI/Application.mk I only have
APP_STL := stlport_shared
I have tried stlport_static and the gnustl_static/shared and all get the same result.
In my Android.mk I have
LIB_TEST_DIR := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PATH := $(LIB_TEST_DIR)
LOCAL_MODULE := testmod
LOCAL_MODULE_FILENAME := libtestmod
LOCAL_CPP_EXTENSION := .cxx .cpp .cc
LOCAL_C_INCLUDES := $(LOCAL_PATH)/libtest2/
LOCAL_CFLAGS := -DHAVE_CONFIG_H
LOCAL_SRC_FILES := test.cc
include $(BUILD_SHARED_LIBRARY)
In a header file that test.cc includes, I have
#include <cassert>
#include <algorithm>
#include <iosfwd>
But, when I build, it cannot find any of these. Any idea what I am doing wrong?
When I do a clean, I see this:
Clean: addiJNI [armeabi]
Clean: stlport_shared [armeabi]
Clean: stlport_static [armeabi]
So, it seems like it knew I wanted stlport. Similar thing happens if I use the gnu libs instead. But when I build even the simplest example, I get something like...
jni/test.cpp:4:19: error: cassert: No such file or directory
jni/test.cpp:6:21: error: algorithm: No such file or directory
jni/test.cpp:7:18: error: iosfwd: No such file or directory
make: *** [obj/local/armeabi/objs/addiJNI/addiJNI.o] Error 1
This was the problem, not sure why.
LOCAL_CPP_EXTENSION := .cxx .cpp .cc
It didn't like the .cxx at the beginning.
I m trying to build an app with android-froyo source in which I am using skia and stl templates,
I have included
MY_INCLUDES=external/zlib external/jpeg external/freetype/include \
frameworks/base/core/jni/android/graphics external/skia/include/core \
external/libpng external/expat/lib <b>external/stlport/stlport</b>
libstlport_cflags := -D_GNU_SOURCE
libstlport_cppflags := -fuse-cxa-atexit
LOCAL_CPPFLAGS := $(libstlport_cppflags)
include $(BUILD_STATIC_LIBRARY)
I get the following error when i try to build the android source with this app, which i kept at packages/apps:
external/stlport/stlport/stl/_new.h:47:50: error: libstdc++/include/new: No such file or directory
Please guide me to rectify this issue.
Thanks
Mohit
As I understand the file which cannot be found by preprocessor is located in bionic folder.
I had the same issue and I solved it by adding the following line:
LOCAL_C_INCLUDES += bionic
I haven't tried this with Android 2.2 but I'm using Android Kitkat (4.4).
To get the stlport library working with our project we included it in our project's Android.mk as so:
include external/stlport/libstlport.mk
This is assuming that on Froyo, there is a libstlport.mk file to include in your build process. In 4.4, there is also a Android.mk file but that builds other code as well and builds stlport as a static library (which is not what we wanted).
You may need to also add the include directory as well, something like: external/stlport/stlport.
cpp
#include <stdio.h>
// The code
// The set of definitions and includes for STLPort
// They used defined() instead of #ifdef.
#define _STLP_HAS_INCLUDE_NEXT 1
#define _STLP_USE_MALLOC 1
#define _STLP_USE_NO_IOSTREAMS 1
#include <stl/config/_android.h>
#include <map>
#include <string>
int main(void)
{
std::string a = "abc";
printf("%s",a.c_str());
return 0;
}
Android.mk
# A simple test for the minimal standard C++ library
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := test-libstl.cpp
LOCAL_C_INCLUDES += sources/cxx-stl/stlport/stlport
LOCAL_SHARED_LIBRARIES += libstlport
LOCAL_MODULE := test-libstl
include $(BUILD_EXECUTABLE)