21

A simple question (i am using android NDK r6 with cygwin, but this is a question regarding makefiles and gcc). Suppose that i put under jni/ directory a library under the dir mylib_v_1/. Mylib is structured as:

mylib_v_1
   mylib
      include

Under the include directory there are two files, myinc1.hpp and myinc2.hpp. In myinc1.hpp there is a line as:

#include <mylib/myinc2.hpp>

in my .cpp file, under the jni/ directory, there is the following line:

#include <mylib/myinc1.hpp>

I want to setup the Android.mk (or what other files needs to be set up) in order to let gcc to know to use, as additional include directory, jni/mylib_v_1/ in order to use #include with brackets (instead of two files, in my real case there are a lot of .hpp that includes a lot of other .hpp with brackets notation).

How can i do this?

Thx.

Ps. If, in the .cpp file i change the include in this way:

#include "mylib/myinc1.hpp"

gcc find myinc1.hpp but, while processing it, it find the second include:

#include  <mylib/myinc2.hpp>

and stop there, saying that it is not able to find the file myinc2.hpp.

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
Luke
  • 641
  • 3
  • 8
  • 14

2 Answers2

26

Option 1:

Add one of the following lines to your Android.mk inside a module of your choice:

   LOCAL_C_INCLUDES := /path/to/your/includes # ignore previous includes
                                              # OR
   LOCAL_C_INCLUDES += /path/to/your/includes # preserve previous includes

If necessary you could create an environment variable pointing at '/path/to/your/includes' and include it like this:

   LOCAL_C_INCLUDES := $(MYLIB_INCLUDES_PATH) # ignore previous includes
                                              # OR
   LOCAL_C_INCLUDES += $(MYLIB_INCLUDES_PATH) # preserve previous includes

Option 2:

  1. Copy the complete folder with all header-files in it (mylib) into the 'jni' folder of your project.

  2. Add the following line to your Android.mk inside a module of your choice:

    LOCAL_C_INCLUDES := $(LOCAL_PATH)/mylib
    

    or

    LOCAL_C_INCLUDES += $(LOCAL_PATH)/mylib
    

Depending on whether there are previous includes or not.

Option 3:

Install the CDT plugin for Eclipse and add the absolute path to the 'mylib' directory to the include paths of your project. Here is a great tutorial that shows all the necessary steps.

http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-development/

Ivo
  • 1,768
  • 20
  • 19
  • 1
    Option 1 and 2 don't work for me on unless I have -I at the beginning of each c include, like _-I/path/to/your/includes_ or _-I"/path/to/your/includes"_. – leetNightshade Aug 18 '13 at 00:20
  • Only use *absolute* path in "LOCAL_C_INCLUDES +=" works for me for CDT case, though there is no such limit for ndk-build. – Yingpei Zeng Apr 26 '16 at 03:43
3

You can add C include paths in you Android.mk using:

common_CFLAGS := -Ijni/mylib/include

Any additional paths require another -I option.

Burton Samograd
  • 3,652
  • 19
  • 21
  • No way to make it work. It still says that can't find mylib/myinc1.hpp. I have added common_CFLAGS += $(LOCAL_PATH)/mylib, since LOCAL_PATH should be the jni directory. – Luke Sep 08 '11 at 15:11
  • From your comment it looks like you forgot the -I before the path. – Burton Samograd Sep 08 '11 at 16:13
  • I have tried all possible combinations.Nothing to do. Really embarassing...However, the project is located under d:/android/workspace/helloworld. Under the helloworld there is the jni directory and in it the .cpp and .mk files. With cygwin, i have to go exactly in the helloworld/jni directory and type: /cygdrive/d/android/android-ndk-r6/ndk-build NDK_LOG=1. Hope this help you guys the help me!! – Luke Sep 08 '11 at 16:52
  • 2
    Ok, i am learning things here. First, i have lauched NDK_BUILD V=1. This will force the bulding process to show how gcc is invoked. I have tried to use common_FLAGS and LOCAL_C_INCLUDES without any success (indeed, no -I cmd line option showed /mylib => this is the real problem). From the docs i have learned another way to include things: now i use LOCAL_C_INCLUDES := $(NDK_APP_PROJECT_PATH)/jni/mylib. It worked immediately. But it is suggested to use, instead, LOCAL_C_INCLUDES. Damn... – Luke Sep 08 '11 at 18:23
  • thanks Luke, your code worked...had the same problem...sat for an hour figuring out this thing...gosh – Houston Feb 05 '13 at 16:43
  • I had to put double quotes around my include since there are spaces in my path names – Randall Nov 27 '16 at 15:31