1

I am writing a C++ code to execute 'top' command in android device. This is the code I use.

using namespace std; int main() { char buffer[1024]; string result; system("top -n 1 | head -n 4 | tail -n 3"); FILE *memcpu= popen("top -n 1 | head -n 4 | tail -n 3","r"); while (!feof(memcpu)) { if (fgets(buffer, 1024, memcpu) != NULL) result+=buffer; } cout<<"result you need\n"<<result; } 

I want to run this file in adb device. Hence I build the program using command

arm-linux-gnueabi-g++ -static -march=armv7-a name.cpp -o test 

When I run the program, the string result is empty.

I tested the program by including system("top -n 1"); line in the program. But I am not getting any output from adb shell (empty string).

I build the same program using g++ and run in linux pc. And at that time I am getting the correct output. What might be the reason that I am not getting desired output in adb shell from android device?

4
  • Are you running this c++ code using JNI or any other way? Commented Aug 11, 2017 at 10:08
  • I am not using JNI. Basically I just build this program to device compatible executable file and put this executable to the system/bin folder using 'adb push' command. then I enter adb shell and execute the program from 'bin'. Commented Aug 11, 2017 at 10:14
  • Are all 3 programs (top, head and tail) available on your test device? Commented Aug 11, 2017 at 13:21
  • Yes, they are. I am able to execute these commands from shell. Commented Aug 12, 2017 at 17:16

1 Answer 1

1

When you build the program using command

arm-linux-gnueabi-g++ -static -march=armv7-a name.cpp -o test 

a static binary is created. In order to link the libraries in android, the program must be built using android ndk build. And that solved the problem for me.

Sign up to request clarification or add additional context in comments.

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.