5

I'm trying to compilate my own library with the Android NDK But I've got some problems.

Here is my Android.mk file:

# Define vars for library that will be build statically.
include $(CLEAR_VARS)
LOCAL_MODULE     := MyLib
LOCAL_SRC_FILES  := ../../../src/mylib/utils/Timer.cpp
LOCAL_C_INCLUDES := ../../../src/mylib/

# Optional compiler flags.
LOCAL_LDLIBS     = -lz -lm
LOCAL_LDLIBS    := -llog  
LOCAL_CPPFLAGS  := -std=c++0x

include $(BUILD_SHARED_LIBRARY)

When I build my project with "ndk-build" I've got the following error :

Clean: mylib [armeabi]
Clean: stlport_shared [armeabi]
Clean: stlport_static [armeabi]
Compile++ thumb  : mylib <= Timer.cpp
jni/../../../src/mylib/utils/Timer.cpp:1:34: fatal error: mylib/utils/Timer.hpp: No such file or directory
compilation terminated.

For information, I'm including the .hpp like that :

#include <mylib/utils/Timer.hpp>

I don't know why headers aren't found, my library working in Xcode and eclipse. Thanks for your time!

Edit: Here is my project's architecture to understand my problem: https://i.stack.imgur.com/wrcAC.jpg I'm trying to indicate where is located my ".hpp" files in the Android.mk file.

Glados
  • 53
  • 1
  • 1
  • 4

2 Answers2

5

Your LOCAL_C_INCLUDES should include the ../../../src/ or ../../../inc directory in order for you to use #include <mylib/utils/Timer.hpp> i.e:

LOCAL_C_INCLUDES := ../../../src/

Why don't you put your C and C++ headers and source files inside the jni/ directory of the Android project, near the Android.mk file?

See: What is the difference between #include <filename> and #include "filename"?

Also this is incorrect, because the second LOCAL_LDLIBS overrides the previous LOCAL_LDLIBS directive in the current module :

LOCAL_LDLIBS     = -lz -lm
LOCAL_LDLIBS    := -llog  

If you want to append to a make directive use:

LOCAL_LDLIBS    := -lz -lm
LOCAL_LDLIBS    += -llog 

or LOCAL_LDLIBS := -lz -lm -llog

EDIT: Using the following Android.mk it seems to work if you run ndk-build from the Android/jni directory.

LOCAL_PATH := $(call my-dir)

# first lib, which will be built statically
#
include $(CLEAR_VARS)
LOCAL_MODULE     := MyLib
LOCAL_C_INCLUDES := ../../../inc/
LOCAL_SRC_FILES  := ../../../src/mylib/utils/Timer.cpp
include $(BUILD_STATIC_LIBRARY)

# second lib, which will depend on and include the first one
#
include $(CLEAR_VARS)
LOCAL_MODULE    := MyNativeFinalSharedLib
LOCAL_LDLIBS    := -lz -lm -llog  
LOCAL_CPPFLAGS  := -std=c++0x
LOCAL_STATIC_LIBRARIES := MyLib   
include $(BUILD_SHARED_LIBRARY)

Also you forgot to put LOCAL_PATH := (call my-dir) on the first line and some other missing make directives.

An Android.mk file must begin with the definition of the LOCAL_PATH variable. It is used to locate source files in the development tree. In this example, the macro function 'my-dir', provided by the build system, is used to return the path of the current directory (i.e. the directory containing the Android.mk file itself).

(from android-ndk-r8d/docs/ANDROID-MK.html)

Community
  • 1
  • 1
Alex Bitek
  • 6,529
  • 5
  • 47
  • 77
  • Thanks for your response, but the line `code`LOCAL_C_INCLUDES := ../../../src/`code` didn't work, I've got the following error: "Unsupported source file extensions". I don't put my .cpp and .hpp files in the same folder or JNI for the architecture clarity, take a look on libraries like SFML who did this too :). In my projects, I'm including the current project in the header search path, so I can include files without searching in subfolders, I can include a "module" too like that "#include Here is my project architecture : [link]http://i.imgur.com/ln3ia9F.jpg – Glados Jan 25 '13 at 15:49
  • In that case have you tried `LOCAL_C_INCLUDES := ../../../inc`? Also where is your Android.mk file located? Is it inside the Android/jni ? Where are you running **ndk-build** from? Your sources should go to LOCAL_SRC_FILES := ../../../src/source1.cpp ../../../source2.cpp Also I suggest that you compile your external code to a shared or static library and then you add a new module for it in the Android.mk file or create a separate file for your library and call it from the main Android.mk inside Android/jni/ directory. – Alex Bitek Jan 25 '13 at 16:17
  • Yes i've tried too :/. Android.mk is inside the jni folder yes, here is another picture with more details [link]http://i.imgur.com/aiah6zH.jpg – Glados Jan 25 '13 at 16:33
  • Thanks! That helped me a lot to find the problem! Now I have to deal with #include during the compilation ^^'. – Glados Jan 25 '13 at 22:38
  • Also check this related discussion: https://groups.google.com/d/topic/android-ndk/VWxNghRtBjY/discussion For using the C++ standard library you need to add the Application.mk file in Android/jni which contains something like: `APP_OPTIM := debug APP_PLATFORM := android-14 APP_STL := gnustl_static APP_ABI := armeabi armeabi-v7a` Also make sure you're using the latest Android SDK: android-ndk-r8d Read: android-ndk-r8d/docs/CPLUSPLUS-SUPPORT.html and check the android-ndk-r8d/samples directory for various examples using the NDK. – Alex Bitek Jan 25 '13 at 22:52
0

It's highly recommended to have your Android.mk file define $(LOCAL_PATH). In your case, the best choice would probably be

LOCAL_PATH := $(call my-dir)/../../../../src

Now you can simply write

LOCAL_SRC_FILES  := mylib/utils/Timer.cpp

But for the includes, the path should be defined as relative from current, i.e. MyProject/build/Android directory. In your case, it seems, the correct path would be

LOCAL_C_INCLUDES := ../../../inc

This when you specify mylib in the #include statement:

#include <mylib/utils/Timer.hpp>
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • **bold**the path should be defined as relative from current, i.e. MyProject/build/Android directory.**bold** That was one of the main problem, thanks a lot! (I had another problem with a bad include in one of my .cpp files, Xcode doesn't say anything strangely. – Glados Jan 25 '13 at 22:39