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.
Related
I am using getline function and compiling using ndk but i am getting error :
'getline' was not declared in this scope
is this error due to limitations of armeabi-v7a or due to glib?How can it be resolved for the same function.
I have already #define _GNU_SOURCE before <stdio.h>
In general, when you encounter such an error, you go to your NDK directory and user either Midnight Commander (Linux) or Far Manager (Windows, Linux+Wine) to search the files (file mask: *.h) for your function, getline in this case. You will get a screenful of search results, and it's up to you to #include the right file.
Once in a while your function will not be found; in this case you search the 'net for a place where you can borrow the source.
Sometimes the function in the code being ported has just no meaning, e.g. if the function reads a line from stdin but the program going to invoke it is not a command-line utility, there is a problem.
Most likely, the source that you port #define-s switches for Linux, Mac (Darwin) and Windows, you have to choose the right configuration to derive the Android configuration from (and probably the Mac one will be the best).
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
I am looking at the source for the android shell reboot command.
int reboot_main(int argc, char *argv[])
What I am not clear about is if this is a standalone binary, who calls reboot_main() ?
In a standard glibc linked binary, I was expecting to find a "main()" as the entry point for the program.
What am I missing here, could someone help me understand what is going on ?
Thanks,
vj
They are all compiled into one overall executable, with main in https://android.googlesource.com/platform/system/core.git/+/android-4.2.2_r1/toolbox/toolbox.c
Then, based on the actual program name invoked (usually argv[0]) it calls the appropriate method.
The commands are part of the build via the
#define TOOL(name) int name##_main(int, char**);
macro in toolbox.c which is used in the Android.mk file to generate tools.h.
android is a kind of linux,and it must support the posix.But,when it seems not support the syscall,open().
Here is the code for testing,and i compile it via NDK:
#include <unistd.h>
#include <stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
void main(){
int fd;
char pathname[128] = "/data/pwrite.txt";
fd = open(pathname, O_WRONLY);
if(fd==-1){
printf("open fail.\n");
}
perror("/data/pwrite.txt");
}
and the following is the prompt that comes from android:
kaiwii#ubuntu:~$ adb shell /data/pwrite/test1
open fail.
/data/pwrite.txt: No such file or directory
I think that the problem is in the flags - you only use O_WRONLY. But if a file does not exist you should also create it using O_CREAT flag. So if a file does not exist you should call:
fd = open(pathname, O_WRONLY | O_CREAT);
I think the problem is not the syscall open() but the fact that your are trying to access /data. This folder is accessible only for rooted mobiles or in the emulator. Have you tried to put the file into the /sdcard folder?
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?)