Any simple way to log in Android NDK code?

前端 未结 7 2401
無奈伤痛
無奈伤痛 2020-12-02 12:09

I\'m looking for a way to easily debug C code in an Android NDK application using Eclipse. I\'ve read ways to debug the app using gdb or something similar but what I want i

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 12:22

    The easiest way is probably to redirect printf() statements to the system log (based on the "Viewing stdout and stderr" section of the official ADB reference manual.

    Type these 3 commands on a command line:

    adb shell stop
    adb shell setprop log.redirect-stdio true
    adb shell start
    

    Then you can view the output of your "printf()" statements by looking at the "LogCat" window of Eclipse Debugger, or by typing this on a command line:

    adb logcat
    

    Just be aware that since the data is buffered before transferring from the emulator or device, you should definitely flush the stdout buffer, eg:

    printf("Hello, I am %d years old!\n", 30);
    fflush(stdout);
    

    You should then see a log message starting with "I/stdout:"

提交回复
热议问题