Running a "Hello World!" ANSI C program on Android? [duplicate] - android

This question already has answers here:
How do I build a native (command line) executable to run on Android?
(4 answers)
Closed 9 years ago.
I don't know if that's even possible, but suppose I want to run a Hello World ANSI C program, how do I go about compiling, deploying and running it? Is it even possible?
Here's the example code of the program I have in mind:
#include <stdio.h>
int main(void)
{
FILE *f = fopen("/storage/sdcard0/download/hello_android.txt", "w");
if (f) {
fprintf(f, "File created by an ANSI C program.");
fclose(f);
}
return 0;
}
As you can see, it should create a file as "proof" that it has run successfully.

1. Compiling
You just have to known your hardware architecture, find the correct toolchain and cross-compil it with the static option. If you don't want to use the static option you have to link your binary to bionic and not libc. It means that you have to compile android from scratch, which will compile bionic. Then you could add your C files into the "android environement", create a Android.mk file and run mm.
2. Deploying
You have to use adb to push your binary to your phone. Something like:
adb push mybinary /data/
3. Running
Launch a shell using adb shell and simply run:
$ /data/mybinary
NOTE: To do step 2 and 3 your phone must be rooted

Related

Libgit2 running on Android unable to perform clone operation - returns "failed to set permissions" error

I have just built Libgit2 (v0.20.0) for Android (target SDK version 18, debugging on a rooted device running Cyanogenmod 10.1.2, Android 4.2.2) and a simple function like getting the version number of Libgit2 works fine through the JNI. But when I use the git_clone function it stops right after the objects/info folder is created and returns this error:
Error -1 cloning repository - Failed to set permissions on '/storage/sdcard0/it/ptt/.git/objects/info': Operation not permitted
I have given the application the WRITE_EXTERNAL_STORAGE permission but I guess it still can't chmod unless owner of the file. When I use adb shell to check out the permission mode of the info folder I get:
d---rwxr-x system sdcard_rw 2014-05-15 09:31 info
And by using pwd.h functions I get the username that the c code (that is calling git_clone) is under to be u0_a92. How am I suppose to get pass this I suppose very Android related issue? Is there a simple way to stop Libgit2 from calling p_chmod or can I give it permissions to do so?
I ended up defining p_chmod as a method always returning true to get passed the error. In the bash script I use to build libgit2 I inserted the following lines that leaves the source files in an unmodified condition after building for android:
LIBGIT2_POSIX_PATH="$LIBGIT2_SOURCE_PATH/src/posix.h"
LIBGIT2_POSIX_BACKUP_PATH="$LIBGIT2_SOURCE_PATH/src/posix_original.h"
printf "#include \"always_success.h\"\nint always_success() { return 0; }" > "$LIBGIT2_SOURCE_PATH/src/always_success.c"
printf "int always_success();" > "$LIBGIT2_SOURCE_PATH/src/always_success.h"
cp $LIBGIT2_POSIX_PATH "$LIBGIT2_POSIX_BACKUP_PATH"
sed -i "s/^#define\sp_chmod(p, m).*$/#include \"always_success.h\"\n#define p_chmod(p, m) always_success()\nextern int always_success();\n/" $LIBGIT2_POSIX_PATH
# run the build process with cmake ...
# restore chmod manipulated source header
mv $LIBGIT2_POSIX_BACKUP_PATH $LIBGIT2_POSIX_PATH
There is probably a cleaner way to solve this but at least now I dont get that error anymore. Thanks to Carlos for his help!
UPDATE
Running adb shell mount | grep sdcard I could see that the sdcard which I am trying to clone the repository into uses the vfat file system which according to this forum thread doesn't support unix-style permissions.

Compiling two files in CCTools Android (GCC Addon installed)

I have 3 files,
1) q5.c
2) q5.h
3) q5fun.c
The contents of each file is listed below,
q5.c
#include <stdio.h>
#include "q5.h"
int number;
main() {
number = 2;
printf("%d\n",fun(5));
printf("%d\n",number);
}
q5.h
extern int number;
int fun(int);
q5fun.c
#include "q5.h"
int fun(int x) {
return x + number++;
}
On linux, code works fine. I am trying to make my platform mobile on a ground vehicle and I wanted to do such computations on my phone.
I installed CCTools and the GCC add-on to it,
When I compile the main program "q5.c", the compiler throws up saying "undefined reference to 'fun'. What is the terminal command am I suppose to execute?
In unix platform I run "cc q5.c q5fun.c"
Under android platform, what am I suppose to run?
Okay seems I can compile and produce the output the way I want. Now the problem is that a.out file does not have execute permission. Any quick tips on how to solve?
--
Execution from sdcard disabled by default for non-rooted devices. Copy your file to terminal home directory (it defined as /data/data/com.pdaxrom.cctools/root/cctools/home), change permission and run it:
cp myprogram ~/
chmod 755 ~/myprogram
~/myprogram
Also, you can use Makefile to compile two or more sources files.

how to convert pdf to pksmraw by using ghostscript in android system?the error code is -15

I want to convert pdf to pksmraw by using ghostscript9.04, the command is:
gs -q -dBATCH -dSAFER -dQUIET -dNOPAUSE -sPAPERSIZE=a4 -r300x300 -sDEVICE=pksmraw -sOutputFile=printjob.pksmraw printjob.pdf
But in android system, it's no output, and the error code is -15.
I think it's maybe Resources and libs is can't be found.
So in android system, how to set up the Resources and libs?
error code -15 is a rangecheck error. In order to find out more you are going to have to find out where stdout and stderr are going, and capture them.
We don't supply Ghostscript for Android systems, so I assume you built this yourself, in which case it should be reasonably straight-forward for you to figure out where the error messages are going.
Oh, the current version is 9.10, you might well want to upgrade too.
Library and resource files are ordinarily built into the executable using a rom file system, unless you specified COMPILE_INITS=0 at build time.
Try this example
gs \
-sDEVICE=pdfwrite \
-o foo.pdf \
/usr/local/share/ghostscript/8.71/lib/viewjpeg.ps \
-c \(my.jpg\) viewJPEG
reads my.jpg and produces foo.pdf. You will have to find where your installation installed the PostScript program viewjpeg.ps.
Same way do it for to convert pdf to pksmraw it will work for you.

Android NDK - Library not found CANNOT LINK EXECUTABLE - how to set LD_LIBRARY_PATH?

I have an Android activity where I'm executing NDK compiled code (command line program) with:
Runtime.getRuntime().exec(myCommand);
and load the needed shared libraries with:
static {
System.loadLibrary(myLib);
}
but when running my project and printing the output from error stream I get the following error:
link_image[1963]: 7520 could not load needed library 'libmyLib.so' for './myCommand'
(load_library[1105]: Library 'libmyLib.so' not found)CANNOT LINK EXECUTABLE
I assure libmyLib.so does exist in my project under libs/armeabi/ directory and it's copied to my Android device under /data/data/myProject.path.package/lib/ directory. Owner and group of both executable and library are system:system and permissions are ok as well.
When executing the command from adb shell in the beginning I get the same error but then I can set LD_LIBRARY_PATH and it runs ok:
./adb shell
export LD_LIBRARY_PATH=/data/data/myProject.path.package/lib:$LD_LIBRARY_PATH
/data/data/myProject.path.package/myCommand
So the question is, how to do that from java Android project?
Note: I'm using Linux, Eclipse + Sequoyah, NDK-r5b, Android 2.3.6 (API 10) on GT-P1010.
Solved: you can use exec(String prog, String[] envp). In my case this was:
String[] envp = {"LD_LIBRARY_PATH=/data/data/cse.ecg.dcmtk/lib:$LD_LIBRARY_PATH"};
Runtime.getRuntime().exec(myCommand, envp);
The issue now is that with exec() the process executed can't resolve hostnames (it does from adb shell; Internet permission is set). Any hint about that?

Pure C++ program compiled for Android

I want to compile this program for Android and see it run on my phone:
#include "Hello World.h"
using namespace codewerks;
//=============================================
// Main Loop
//=============================================
int main(int argc, char* argv[])
{
Print(std::string("Hello World!"));
}
Where do I start? Can I compile this with GCC? The NDK seems focused on Java. Thank you.
This is now possible with the latest NDK. You will need an emulator or phone running Android 2.3 to try it, but the NativeActivity documentation has a complete example.
Unfortunately it is somewhat more complicated than a simple "hello world" example, and "main" is spelled "android_main". You still need to worry about your application life cycle as you do in Java, and the only real way to draw to the screen is to use OpenGL ES. It seems to be designed for writing games for Android.
Build as executable. (BUILD_EXECUTABLE)
Copy the executable to sdcard. (adb push)
Go to android shell. (adb shell)
Change the permission of the executable. (chmod 777)
Run the executable. (./out)
You will see the printed result on the console. (happy?)

Categories

Resources