Build Android.mk with GNU Makefile

6,574 views
Skip to first unread message

kaushik mv

unread,
May 27, 2011, 7:11:08 AM5/27/11
to andro...@googlegroups.com
I have a huge set of c files with many makefiles in it. I have tried a lot to build Android.mk file with the help of those Makefiles but none of them did not work. What is the simplest way to write Android.mk file for it?



Olivier Guilyardi

unread,
May 27, 2011, 8:26:58 AM5/27/11
to andro...@googlegroups.com
What doesn't work? If you don't give any details it isn't easy to help.

Also, why don't you try and use the standalone toolchain with your existing
makefiles instead of writing Android.mk files?

See: ndk/docs/STANDALONE-TOOLCHAIN.html

Olivier

> --
> You received this message because you are subscribed to the Google
> Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to
> android-ndk...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/android-ndk?hl=en.

kaushik mv

unread,
May 27, 2011, 8:54:34 AM5/27/11
to android-ndk
I am new to android. My make file.am is like this

EXTRA_DIST = Makefile.mak

INCLUDES = -I$(top_srcdir)/include

bin_PROGRAMS = hts_engine

hts_engine_SOURCES = hts_engine.c

hts_engine_LDADD = ../lib/libHTSEngine.a

DISTCLEANFILES = *.log *.out *~

MAINTAINERCLEANFILES = Makefile.in

By looking at this how can I write android.mk file?
Can you throw some light on how to use standalone-toolchain?


thanks,
Kaushik

Olivier Guilyardi

unread,
May 27, 2011, 12:03:22 PM5/27/11
to andro...@googlegroups.com
Well, this is only one Makefile.am amongst others I guess.

The thing is that there is no recipe for writing an Android.mk for an existing
library. Each case is a bit specific.

Using the standalone toolchain should be easier than Android.mk files here.
Provided that you built the toolchain in /path/to/toolchain, then you should be
able to do something like:

export PATH=/path/to/toolchain/bin:$PATH
export CC=arm-linux-androideabi-gcc
export LD=arm-linux-androideabi-ld
export AR=arm-linux-androideabi-ar

./configure

And then just type make.

Olivier

kaushik mv

unread,
May 27, 2011, 12:50:31 PM5/27/11
to android-ndk
when I try to compile a C file with arm-linux-androideabi-gcc i am
getting the following error

C file "1.c" :
#include<stdio.h>
int main()
{
return 0;
}

In file included from 1.c:1:
./../lib/gcc/arm-linux-androideabi/4.4.3/include-fixed/stdio.h:50:23:
error: sys/cdefs.h: No such file or directory
./../lib/gcc/arm-linux-androideabi/4.4.3/include-fixed/stdio.h:51:24:
error: sys/_types.h: No such file or directory
In file included from 1.c:1:
./../lib/gcc/arm-linux-androideabi/4.4.3/include-fixed/stdio.h:163:
error: expected '=', ',', ';', 'asm' or '__attribute__' before
'extern'
./../lib/gcc/arm-linux-androideabi/4.4.3/include-fixed/stdio.h:232:
error: expected '=', ',', ';', 'asm' or '__attribute__' before
'__BEGIN_DECLS'
./../lib/gcc/arm-linux-androideabi/4.4.3/include-fixed/stdio.h:382:
error: expected '=', ',', ';', 'asm' or '__attribute__' before
'__BEGIN_DECLS'
./../lib/gcc/arm-linux-androideabi/4.4.3/include-fixed/stdio.h:393:
error: expected '=', ',', ';', 'asm' or '__attribute__' before
'static'


Help me to debug this

Thanks,
kaushik

Olivier Guilyardi

unread,
May 27, 2011, 1:06:13 PM5/27/11
to andro...@googlegroups.com
Have you read ndk/docs/STANDALONE-TOOLCHAIN.html?

I especially recommend that you follow the instructions in section 3
"Invoking the compiler (the easy way)".

Olivier

kaushik mv

unread,
May 27, 2011, 1:11:52 PM5/27/11
to android-ndk
yes. My problem is that when I try to compile any C file with arm-
linux-androideabi-gcc I am getting the error stated above. Why is
that happening?

Thanks,
kaushik.

kaushik mv

unread,
May 27, 2011, 1:41:56 PM5/27/11
to android-ndk
Now when I do ./configure then I am encountering the following error

checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... /home/kaushik/Downloads/android-ndk-r5b/toolchains/
arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-
androideabi-gcc
checking for C compiler default output file name...
configure: error: in `/home/kaushik/t/appfolder':
configure: error: C compiler cannot create executables
See `config.log' for more details.

"/home/kaushik/t/appfolder' is the application folder in which I am
executing ./configure

Thomas Fjellstrom

unread,
May 27, 2011, 2:22:16 PM5/27/11
to andro...@googlegroups.com

If you look at the docs to create a standalone toolchain, using the ndk
toolchain dir doesn't really work. You absolutely need to run the tool to
create a real standalone toolchain and then use that as your toolchain root.

--
Thomas Fjellstrom
tfjel...@gmail.com

kaushik mv

unread,
May 27, 2011, 2:54:41 PM5/27/11
to android-ndk
Actually I executed the following sequence 0f commands..


SYSROOT=/home/kaushik/Downloads/android-ndk-r5b/platforms/android-8/
arch-arm
/home/kaushik/Downloads/android-ndk-r5b/build/tools/make-standalone-
toolchain.sh --platform=android-5 --install-dir=/tmp/my-android-
toolchain
export PATH=/path/to/toolchain/bin:$PATH
export CC=/home/kaushik/Downloads/android-ndk-r5b/toolchains/arm-linux-
androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc
export LD=/home/kaushik/Downloads/android-ndk-r5b/toolchains/arm-linux-
androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-ld
export AR=/home/kaushik/Downloads/android-ndk-r5b/toolchains/arm-linux-
androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-ar

then i executed ./configure

am I missing something?

Olivier Guilyardi

unread,
May 27, 2011, 3:05:22 PM5/27/11
to andro...@googlegroups.com
Now, you have built the toolchain in /tmp/my-android-toolchain, and you are
setting CC to the path of the NDK internal toolchain gcc... How can that be?

Also, you don't need to repeat the path in every CC, LD, AR, etc.. variable,
that's what PATH is here for.

/path/to/toolchain was just a placeholder, as an example.

Try:

export PATH=/tmp/my-android-toolchain/bin:$PATH


export CC=arm-linux-androideabi-gcc
export LD=arm-linux-androideabi-ld
export AR=arm-linux-androideabi-ar

./configure

Olivier

kaushik mv

unread,
May 27, 2011, 3:17:00 PM5/27/11
to android-ndk
I did that as you said. Now I am encountering the following error when
i execute ./configure in /home/kaushik/t/appdir
----------------------------------------------------------------------------------------------
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... arm-linux-androideabi-gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... configure: error: in `/home/
kaushik/t/appdir':
configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details.
------------------------------------------------------------------------------------------------
please let me know where I am going wrong

Thanks in advance

Olivier Guilyardi

unread,
May 27, 2011, 3:46:05 PM5/27/11
to andro...@googlegroups.com
Try with:

./configure --host=arm-eabi

That's needed to tell autoconf that you're cross compiling.

kaushik mv

unread,
May 27, 2011, 4:06:12 PM5/27/11
to android-ndk
Thank you for everything :) Now how can I use the executable in
android?

kaushik mv

unread,
May 28, 2011, 6:23:44 AM5/28/11
to android-ndk
Please tell me how to use the executable file in android.

Thanks,
kaushik.

Thomas Fjellstrom

unread,
May 28, 2011, 2:53:39 PM5/28/11
to andro...@googlegroups.com

If its a regular executable, you can't at least not if you want it to act like
a regular android application. Apps have to use Android API's to work as
normal Android Apps.

--
Thomas Fjellstrom
tfjel...@gmail.com

kaushik mv

unread,
May 28, 2011, 10:22:46 PM5/28/11
to android-ndk
It is not normal executable. It is the executable created after using
the ndk standalone toolchain, How can I use this?

thanks,
kaushik.

alan

unread,
May 30, 2011, 3:23:15 AM5/30/11
to andro...@googlegroups.com
As stated by Thomas you cannot distribute executables as native applications, all you can do with them is deploy them to your device using adb then execute them inside an adb shell

kaushik mv

unread,
May 30, 2011, 4:25:05 AM5/30/11
to android-ndk
I made an application using the tool chain. What should I do if I want
the application to be used by anyone?

Olivier Guilyardi

unread,
May 30, 2011, 8:29:37 AM5/30/11
to andro...@googlegroups.com
Hello,

You haven't "made an application". You have compiled an executable and/or native
libraries.

Now, if you haven't done already, you need to create an Android application,
certainly in Java, to provide a user interface, with buttons, etc...

And this application must either call native functions of your library through
JNI, or run the executable with the appropriate arguments from Java with
Runtime.exec().

Olivier

kaushik mv

unread,
May 30, 2011, 1:45:57 PM5/30/11
to android-ndk
I used Runtime.exec() to execute the command from the code. Now what
are the deliverables? How should I deploy this in android?

thanks,
kaushik.

Olivier Guilyardi

unread,
May 30, 2011, 2:57:29 PM5/30/11
to andro...@googlegroups.com
Copy the executable into <project>/libs/armeabi/. When the app is installed,
this executable will be automatically copied into <datadir>/lib/, where
<datadir> can be obtained with:

PackageManager pm = context.getPackageManager();
String dataDir = pm.getApplicationInfo(context.getPackageName(), 0).dataDir;

Olivier

kaushik mv

unread,
May 30, 2011, 6:18:32 PM5/30/11
to android-ndk
Do I need to manually create libs folder? Which files do I need to
copy there?

thanks,
kaushik.

Olivier Guilyardi

unread,
May 31, 2011, 7:29:13 AM5/31/11
to andro...@googlegroups.com
Yes, since you don't use Android.mk, you need to create libs/armeabi/ manually.
If your executable is statically linked with the libraries that it uses, then
the executable is the only thing that you need to put in there.

Beware that if you ever target the armeabi-v7a ABI (to take advantage of the
FPU, etc..), then the binaries need to be placed into libs/armeabi-v7a/ instead.
But if you don't pass any flags about ARMv7 when compiling, then you don't need
to care about this.

Olivier

kaushik mv

unread,
May 31, 2011, 8:08:32 AM5/31/11
to android-ndk
Does copy of the executable file into "<datadir>/lib/" takes place
only if we install it on mobile? I mean when I run the code I am
getting <datadir> as "/data/data/com.<appname>" and I cannot find my
executable file in "<datadir>/lib/" that is in "/data/data/
com.<appname>/lib/" .

thanks,
kaushik.

Olivier Guilyardi

unread,
May 31, 2011, 8:13:14 AM5/31/11
to andro...@googlegroups.com
When you "run the code" on what? The emulator? Should work too.

Olivier

kaushik mv

unread,
May 31, 2011, 9:11:03 AM5/31/11
to android-ndk
I might be missing something. Now what I did is I placed the
executable file in assets folder and then I wrote java code to copy
files from assets folder to the com.<application_name> folder and then
executing it. Does this create any problem or is it fine?

thanks,
kaushik.

Olivier Guilyardi

unread,
May 31, 2011, 10:30:47 AM5/31/11
to andro...@googlegroups.com
Ah, I just made a couple of tests and the files in libs/armeabi/ which are not
named lib*.so are ignored. That's odd, since it might affect executable built
using Android.mk as well.

Your method is a good alternative IMO. But make sure that your properly handle
failures in case the filesystem is full.

Olivier

kaushik mv

unread,
May 31, 2011, 10:45:40 AM5/31/11
to android-ndk
Now I am neither able to change the file permissions of the executable
file nor able to execute it.
I used just the following command to change the permissions of the
file but it did not work for me.
Process process = Runtime.getRuntime().exec("/system/bin/chmod 744 /
data/data/com.<app_name>/<file_name>");

Even if I manually changed the permissions of the file it is not
getting executed and I am getting the following error message.

Error running exec().Command[/data/data/com.<my_app_name>/
<my_executable_file_name>]
Working Directory:null Environment:null.

Help me to debug this.

thanks,
kaushik.

Olivier Guilyardi

unread,
May 31, 2011, 11:52:54 AM5/31/11
to andro...@googlegroups.com
Don't put the executable at the datadir root, put it in a subdirectory, such as
<datadir>/bin/

Olivier

kaushik mv

unread,
May 31, 2011, 2:33:57 PM5/31/11
to android-ndk
This problem is seriously irritating me. Now I cannot even copy files
to any directory except to that of "/sdcard/" where I cannot execute
any file. I am using the following code to copy files.
private void CopyAssets() {

AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
for(int i=0; i<files.length; i++) {
InputStream in = null;
OutputStream out = null;

try {

in = assetManager.open(files[i]);
PackageManager pm = getPackageManager();
String dataDir = pm.getApplicationInfo(getPackageName(),
0).dataDir;
String S =dataDir+"/";

out = new FileOutputStream(S+ files[i]);


copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {

Log.e("tag", e.getMessage());
}
}
}
private void copyFile(InputStream in, OutputStream out) throws
IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
It worked before but now files are not getting copied to the /data/
data/com.<app_name> .Please help me!!!

thanks,
kaushik.

kaushik mv

unread,
May 31, 2011, 2:37:50 PM5/31/11
to android-ndk
oh, sorry got it. I had to change the permissions of /data/data/
com.<app_name>.

thanks,
kaushik.

kaushik mv

unread,
May 31, 2011, 2:48:37 PM5/31/11
to android-ndk
There is no bin folder in <datadir> directory. I manually created it.
Even then changing permissions of files in <datadir>/bin/ did not
work. Please suggest an alternative.

thanks,
kaushik.

Olivier Guilyardi

unread,
May 31, 2011, 2:59:03 PM5/31/11
to andro...@googlegroups.com
Yes, indeed, I suggested that you create this bin/ folder or any other folder in
<datadir>, before copying the executable in it.

You may need to run chmod with Runtime.exec() to adjust the permissions of the
executable. In doubt try 777.

I did test on Android 2.1 and it works.

Olivier

kaushik mv

unread,
May 31, 2011, 3:14:22 PM5/31/11
to android-ndk
I tried the following
dataDir = pm.getApplicationInfo(getPackageName(), 0).dataDir;
String S =dataDir+"/";
Process process = Runtime.getRuntime().exec("/system/bin/chmod 777 "+S
+"bin/*");

But it did not work. I kept the permissions of bin folder as 777. I am
using android 3.1 . Did u do the same thing as I did ?

kaushik mv

unread,
May 31, 2011, 3:38:23 PM5/31/11
to android-ndk
Sorry,it works. I actually did like chmod <appdir>/bin/* and there are
so many files in bin (the files which executable uses). So may be it
will take time to change permissions of all files. I just changed the
permissions of <appdir>/bin/<exe file> and it worked.

thanks,
kaushik.

Olivier Guilyardi

unread,
May 31, 2011, 3:42:25 PM5/31/11
to andro...@googlegroups.com
No. Try:

String args[] = {"chmod", "777", filepath};
Runtime.getRuntime().exec(args);

Where filepath is the absolute path to your executable.

Olivier

Olivier Guilyardi

unread,
May 31, 2011, 3:49:34 PM5/31/11
to andro...@googlegroups.com
Good! Man, that was a long road ;)

Olivier

kaushik mv

unread,
May 31, 2011, 4:10:07 PM5/31/11
to android-ndk
Now I am unable to execute the file even if it has permissions 777. I
am using the command
Process process = Runtime.getRuntime().exec(<datadir>+"/
bin/"+<executablefile>);
output is that it has to create a wav file but it is not. Please help
me regarding this.

thanks,
kaushik.

kaushik mv

unread,
May 31, 2011, 8:03:44 PM5/31/11
to android-ndk
Whatever folder I chose I always end up creating an empty file (my
output file is empty) in that folder. But when I execute the command
in the shell then it is getting executed properly that is it is
creating a file with data in it.But when I create a C file which
writes some values into a file,compile it with arm gcc and execute it,
it is working properly(both from the shell as well as from the code).
But my actual executable file is not writing properly to a file. This
problem is seriously giving me nightmares. Please help me to solve
this.

Regards,
kaushik.

On Jun 1, 12:49 am, Olivier Guilyardi <l...@samalyse.com> wrote:

kaushik mv

unread,
May 31, 2011, 10:29:45 PM5/31/11
to android-ndk
got it. Things worked when I put all input/output files in /sdcard/
and executed the executable from <datadir>/

thanks,
kaushik.
Reply all
Reply to author
Forward
0 new messages