Fixing Eclipse errors when using Android NDK and std::vector - android

I'm using eclipse to develop an android app that also uses the ndk. I vectors in my app and I've done the necessary stuff to get them by including
APP_STL := stlport_static
In my Application.mk
Everything is working fine it compiles and runs but Eclipse keeps giving me errors when I use the vectors
std::vector<int> blah
for example creates an error. If I delete the error and keep going it compiles and runs fine.
I've added
${NDKROOT}/sources/cxx-stl/gnu-libstdc++/include
In my project config under C++ General -> Paths and Symbols -> include
It resolves #include <vector> fine (before I added the path above I had an error for this) but I still get errors using the vectors.
How can I get eclipse to stop giving me errors for this?
EDIT:
example error: Symbol 'vector' could not be resolved
EDIT 2:
I tried to add
using namespace std;
and then using vector blah and that causes a different error:
Invalid template arguments

I've added ${NDKROOT}/sources/cxx-stl/gnu-libstdc++/include
In my project config under C++ General -> Paths and Symbols -> include
Yes, that's it. I've tried to add the same with the same result.
However, if you add stl_port headers
${NDKROOT}/sources/cxx-stl/stlport/stlport
It will do the trick. Of course it is not necessary to change
APP_STL := stlport_static
as it works only in eclipse indexes. It will be usefull until you are going to use something that exists in gnu-libstdc++ and doesn't exist stl-port.

Blockquote
I am suing eclipse indigo rc2.
I added following line in Android.mk
LOCAL_C_INCLUDES += ${NDKROOT}/sources/cxx-stl/stlport/stlport
and Added following line in Application.mk
APP_STL := stlport_static
then automatically my ndk stlport path added in
Properties -> C++ General -> Paths and Symbols -> include
good luck! ^^

At first, we met the same problem with map and tried to add "Paths and Symbols" as suggested, however it still wouldn't work.
Later on, instead of
#include <map>
we used
#include <stl/_map.h>
The error went away and then we switched back to include <map>. Eclipse no longer complained that "Symbol could not be resolved".
It seems eclipse has a cache and somehow it can get messed up unless you specifically tell it the right place to find the symbols.

I do not know at what stage it worked, but:
Add to Application.mk APP_STL := gnustl_static
Add include to Project properties->C/C++ General -> Paths and Symbols 'NDK root path'/'your directory to android platform'/arch-arm/usr/include
'NDK root path'/sources/cxx-stl/gnu-libstdc++/4.9/include
'NDK root path'/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include/bits
Turn off all warnings errors in Project properties->C++ General->Code Analisis.
Project properties->C++ Build-> Builder Settings -> Uncheck use default build command. Build command set empty.
Next configure NDK Builder: Project properties-> Builders-> New-> Program and fill Name (your name build conf), Location (path to NDK root directory), Working directory (path to project dir). -> Refresh and check specific resources (your libs folder in project). -> Build Options check Specify working set of relevant resources and change 'jni' folder with your source.
worked in Ubuntu 15.04. Eclipse 3.8.1. Android NDK r10e.

Related

Libogg compiled with standalone toolchain (generated by NDK script) tries to load itself from incorrect file - libogg.so.0 instead of libogg.so

I'm trying to add libogg to my android NDK project.
I'm using project template that comes with SDL library.
I'm on Windows, so all following scripts were executed under MSYS, a linux terminal emulator.
First, I generated a standalone toolchain.
android-ndk/build/tools/make-standalone-toolchain.sh --platform=android-12 --system=windows-x86_64 --toolchain=arm-linux-androideabi-4.9 --install-dir=Y:/gcc_android_androideabi-4.9_api12/
Then I built the library.
make clean
configure CC="/y/gcc_android_androideabi-4.9_api12/bin/arm-linux-androideabi-gcc" LIBS="-lgnustl_shared" CFLAGS="-mthumb" --host=arm-linux-androideabi
make
Then I copied resulting libogg.so to my project folder and added following to my jni/src/Android.mk:
...
include $(CLEAR_VARS)
LOCAL_MODULE := ogg-prebuilt
LOCAL_SRC_FILES := ../../prebuilt-libs/$(TARGET_ARCH_ABI)/libogg.so
include $(PREBUILT_SHARED_LIBRARY)
...
LOCAL_SHARED_LIBRARIES := ... ogg-prebuilt ...
...
Similar code works fine for another libraries that I use.
ndk-build successfully finds my libraries and adds them to the .apk.
The problem arises when I call System.loadLibrary("ogg"); at runtime. Dynamic linker gives me error message saying something like
Can't find library "libogg.so.0".
I'm sure that my library loading code is ok because it works for other libraries that I have.
Error from the dynamic linker is interesting because make not just generated single libogg.so. It generated 3 completely same files with different names: libogg.so, libogg.so.0 and libogg.so.0.8.2.
I tried to replace libogg.so with libogg.so.0 and adjusted Android.mk properly, but NDK build script yelled at me, saying that prebuilt shared libraries must have .so extension. I tried to just copy libogg.so.0 to libs/ folder, but NDK build script ignored it when building .apk.
Then I opened libogg.so in a hex editor and searched for libogg. I found only one occurence: libogg.so.0[NUL]. I replaced .0 with 2 [NUL]s, so it became libogg.so[NUL][NUL][NUL] and now library loads perfectly.
I can reproduce this error with all toolchains I use: arm-linux-androideabi-gcc-4.9, i686-linux-android-gcc-4.9 and mipsel-linux-android-gcc-4.9. They all are generated using similar scripts.
The error persists if I rename libogg.so.0 or libogg.so.0.8.2 to libogg.so and use it instead of original one. As I said, all three files have same content.
While I can make a sed script to automatically fix library name in .so files when necessary, I wonder if there is a better solution to this problem.
Maybe I have to add some flags to configure?
My slightly less hacky fix for this is to remove the version information from the build system of libogg.
In the directory where you have the libogg source code, open the file src/Makefile.am and change this line:
libogg_la_LDFLAGS = -no-undefined -version-info #LIB_CURRENT#:#LIB_REVISION#:#LIB_AGE#
to
libogg_la_LDFLAGS = -avoid-version
If you don't have automake installed then you can change the generated Makefile after running ./configure. Find the libogg_la_LDFLAGS and make a similar change to the one detailed above. On Linux a sed one-liner like this does the job:
sed -i 's/^libogg_la_LDFLAGS.*/libogg_la_LDFLAGS = -avoid-version/g' src/Makefile

Android NDK mangles relative path on windows

I am trying to create an Android Studio wrapper around C code and am running into a problem with the NDK. Because the C code is from a 3rd party project, I am trying to not move the code location and have the project in a subdirectory of the repository and as such have to not use the build in call to the NDK and its autogenerated make file. The NDK call works correctly, but I get the following error:
make.exe: *** No rule to make target `C:/some_relative_path/jni/../../../../core.c', needed by `C:/some_relative_path/obj/local/armeabi/objs/my_module/C_/some_relative_path/jni/__/__/__/__/core.o'. Stop.
As you can see, the object path has been mangled such that : and .. have been turned into underscores.
I had to add a jni folder to my project and place the Android.mk and Application.mk files in it to satisfy the path appending of the NDK Gradle plugin. As a result the jni folder had no source files in it. Since I found several links on google talking about needing more than one source file, I added two dummy source files to the jni directory.
Among other things, my Android.mk file contains the following:
LOCAL_SRC_FILES := \
$(LOCAL_PATH)/NDKBug1.c \
$(LOCAL_PATH)/NDKBug2.c \
$(LOCAL_PATH)/../../../../core.c \
I'm looking to see if anyone can help me with either this path issue directly, or perhaps suggest an alternative way of setting things up.
The file names in LOCAL_SRC_FILES are supposed to be relative to the local directory, so you need to remove the $(LOCAL_PATH)/ prefix - then it should work.
I haven't heard about a bug requiring to have source files in the local dir though, so I think you can get rid of them.

Proper way to use 3rd party .so files in android

im stuck with this problem and dont know what im doing wrong. Please help me out.
Im working on an android project where I have to use 2 native library(.so) file. These 2 files are not generated by me, i just have to use them in my project.
I have placed these .so files under libs->armeabi folder. In the code i have used System.loadLibrary("name") inside a static block to load these library files. After doing this, i clean the project and run the application. I get the following error
java.lang.UnsatisfiedLinkError:
What am i missing. I have unchecked the first two options in build settings as well.
Note: I am not developing the native library. I am just using it. It was being used in a previous version, so I am pretty sure there is nothing wrong in the way the .so files have been generated.
PS: My ultimate requirement is like this,
My android application will have a jar which I am developing. The methods in the jar will make use of these native functions. The main application will not directly access the native functions. Should i place the .so files in the jar or should it be in the main application. How should i package the jar and .so files.
Please help me out, this is my first time using native functions and im totally lost. Links to any materials would be great. 
Set PATH to your NDK
Eclipse -> Window -> Preferences -> Android -> NDK -> set path to the NDK
Right click on an Android project and select Android Tools -> Add native support.
Make changes to your Android.mk file to include this .so file
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := <module_name>
LOCAL_SRC_FILES := <.so file name>
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../jni/include
include $(PREBUILT_SHARED_LIBRARY)
Clean project and build again

To include c++ headers in Eclipse for Android NDK project(Mac)

I'm working on an Android NDK project in which I have to use functionalities which needs c++ header files such as iostream , sstream , etc,. I have already did this in linux by adding /usr/include/c++/4.7.. in C++ Paths & Symbols tab. But still din't find a way to do this in Mac. I have tried the following:
I have simply included iostream header #include<iostream> , it shows the following error while build using ndk-build, fatal error: iostream: No such file or directory
Added android-ndk-r9/sources/cxx-stl/gnu-libstdc++/4.8/include in C++ Paths and Symbols->Includes tab->cpp , It doesn't change anything.
In my Android.mk file,
LOCAL_C_INCLUDES := android-ndk-r9/sources/cxx-stl/gnu-libstdc++/4.8/include
it gave the following error:
fatal error: bits/c++config.h: No such file or directory
I have also tried adding prebuilt shared library in Android.mk file , which gave the same error.
Then made a search for the file bits/c++config.h , copied it in to the actual place it is looking for, it shows the same error for osdefines.h , I have copied all the files it is looking for , atlast it asked for bits/memoryfwd.h , but I can't find the file anywhere in my Mac.
What is the actual problem here? What should I do to include those headers in cpp files in my Android NDK project?
Create a file called Application.mk in the directory projet_dir/jni/ (so it is projet_dir/jni/Application.mk).
Add the following line to that file
APP_STL:=stlport_static
If you run into a shared_ptr error, try using APP_STL := gnustl_static instead.
(I had this exact same issue on Linux and the above resolved it for me.)

how to set eclipse not delete some files during building

I configure ndk with eclipse in order to build my c++ code automatically. But i have two external .so file inside libs folder. Everytime, eclipse will delete these external .so file automatically when build project. Is it possible to tell eclipse not delete these external file.
The solution is here.
To summarize (and complement):
Copy your external (e.g., libexternal.so) library (or libraries) to another folder inside your 'jni' folder; for example 'myproject/jni/prebuilt'.
Add the following block to your existing 'jni/Android.mk' (one block for each external library):
include $(CLEAR_VARS)
LOCAL_MODULE := libexternal
LOCAL_SRC_FILES := prebuilt/libexternal.so
include $(PREBUILT_SHARED_LIBRARY)
Add 'libexternal' to 'APP_MODULES' in your existing 'jni/Application.mk'. 'APP_MODULES' should already list your JNI module (e.g., 'myjnimodule'):
APP_MODULES := libexternal myjnimodule
Confirm that the following block exists in 'jni/Application.mk'. Use the appropriate target architecture(s):
APP_ABI := armeabi-v7a
The result is that, as part of the invocation to 'ndk-build', the external library is copied to your 'myproject/libs/armeabi-v7a' folder.
The solution given by John Doedoe led me to another possibility, specific to Eclipse (tested on Mars 1 version):
Right click on your project in the project explorer.
Go into properties.
Go into "C/C++ Build".
Go into the "Builder Settings" tab.
Uncheck "Use default build command".
In the "Build command" text box type your target, e.g.: ndk-build APP_ABI="armeabi-v7a"
Apply / Ok
This will avoid changing behavior on a build machine for instance.

Categories

Resources