2

Here is the basic C source code :

#include <stdio.h> int main(void) { puts("hello world!"); return 0; } 

I am able to compile for Android with NDK toolchain with :

aarch64-linux-android21-clang test.c 

It generates an a.out file that I transfer to the Android device with :

adb push a.out /data/local/tmp adb shell chmod 777 /data/local/tmp/a.out 

When I try to run it with:

adb shell /data/local/tmp/a.out 

I have the message : "/system/bin/sh: /data/local/tmp/a.out: No such file or directory"

What am I doing wrong?

9
  • 1
    Waht kind of android device do you have? Is it Arm64 with 64-bit kernel? Commented Jan 29, 2021 at 15:04
  • Yes it is a 64 bit processor phone : Samsung A3 Commented Jan 29, 2021 at 15:06
  • The NDK is not designed for creating standalone binaries. Commented Jan 29, 2021 at 15:07
  • Did you try adb root before push and shell? Commented Jan 29, 2021 at 15:07
  • I have just made a new test with "armv7a-linux-androideabi16-clang test.c" and now it seems to run well on the device but I have a message "WARNING: linker: /data/local/tmp/a.out: unsupported flags DT_FLAGS_1=0x8000001" Commented Jan 29, 2021 at 15:09

2 Answers 2

2

you can use this nodejs script to automate build and test your c/c++ program for android

 let cpp=` #include<iostream> #include<string> using namespace std; int main(){ cout<<"hellow word"<<endl; return 0; } ` let args=['--target=armv7a-linux-androideabi23'] let cpu="armeabi-v7a" wfs('test.cpp',cpp) args.push("test.cpp") args.push("-static-libstdc++") //in case you want to build sharedlib uncomment next line //args.push("-shared") lg=run(runners.clang,args) lg=run(runners.adb,'push a.out /data/local/tmp/a.out') //in case -static-libstdc++ not used uncomment next line; //copy c++_shared to same dir where to a.out //run(adb,'push c++_shared.so /data/local/tmp/c++_shared.so') let shellcmd=[]; shellcmd.push("cd /data/local/tmp") //in case -static-libstdc++ not used uncomment next line; // shellcmd.push("export LD_LIBRARY_PATH=.") shellcmd.push("chmod +x a.out") shellcmd.push("./a.out") // a.out excution result let output=run(runners.adb,`shell "${shellcmd.join("&&")}"`) lg(output) //++++++++++utilis++++++++++ function lg(...args){ console.log(...args) } function run(cmd,args){ if(args) cmd= cmd+" "+args.join(" "); return require('child_process').execSync(cmd); } function wfs(p,data){ require("fs").writeFileSync(p,data) } 
Sign up to request clarification or add additional context in comments.

2 Comments

Please try to format your answers correctly so it will be easier to read your answer and try to explain what your code is doing so the questioner will understand your code easier.
Please annotate to show WHY you do it, to help other people in future
1

In my case, I had to use "armv7a-linux-androideabiXX-clang" for compilation and then it is ok. XX must be replaced by the API version.

On my windows system, clang binaries and scripts are in the folder "D:\AndroidSdk\ndk\21.1.6352462\toolchains\llvm\prebuilt\windows-x86_64\bin"

The most recent script that can be used is "armv7a-linux-androideabi29-clang++.cmd" and it is ok for compiling C sourcecodes for running on Samsung A3 phones in my case.

I think this is because this is not a 64-bit kernel and I figured that out after Eugene Sh asked me in comments to this question (if he adds an answer then I'll accept it).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.