dev-logs

안드로이드에서 C++ 로그 출력하기 본문

Language/Android

안드로이드에서 C++ 로그 출력하기

두룹두두 2017. 12. 27. 12:07

안드로이드에서 native c++ 를 사용할때 로그찍는 방법입니다.


c++에서 흔히 쓰는 printf를 안드로이드에서는 사용할 수 없습니다.


다른 블로그에 나와 있는 ndk를 사용한 방법은 저는 쓸수가 없더라고요ㅜ 다른 방법을 찾아봤습니다.


안드로이드 sdk 자체에서는 c++에서 사용가능한 출력 함수를 제공하고 있습니다.


먼저 sdk폴더에서 함수가 선언돼있는 헤더파일을 포함해줘야 합니다.



안드로이드 sdk 폴더에서

ndk-bundle\sources\android\native_app_glue 경로에 소스가 있습니다. 확인하시구요~!


참고로 안드로이드 sdk폴더는 보통

C:\Users\Admin\AppData\Local\Android\Sdk  에 있습니다~






app폴더에서 CMakeList.txt 파일을 열어줍니다.








1
2
3
# Specifies a path to native header files.
include_directories(${CMAKE_CURRENT_SOURCE_DIR}
                 ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c )
cs


소스를 입력해줍니다. 

위의 경로에서 sdk 다음폴더인 ndk-bundle 부터 쓰시면 됩니다.

ANDROID_NDK 경로가 ndk-bundle 까지니까 /sources/android/native_app_glue/android_native_app_glue.c




소스위치는 add_library 가 끝나고 난 다음이 좋은것 같습니다. 어쩌면 상관없을지도 모르겠어요..ㅎㅎ 제 추측입니다.








1
2
3
4
5
6
7
8
target_link_libraries( # Specifies the target library.
                       native-lib
                       app-glue
                       lib_opencv
 
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )
cs


target_link_libraries 에 app-glue를 추가해줍니다.

저는 opencv랑 c++을 사용해서 native-lib, lib_opencv 가 추가돼있습니다.




마지막으로 gradle sync 를 맞춰서 성공하면 끝입니다!






추가로 쓰는 방법은 다음과 같습니다.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Main.cpp
 
#include <android_native_app_glue.h>
#include <android/log.h>
 
////////////////////////////////////////////////////////////////////////////////
// The main entry into the game.  2016/06/30 DK
void android_main(
  android_app *pstDroidAPI) // The pointer's allocated memory helps manage the
                            // interface between the Android OS and this game
{
  // The function call prevents the linker from
  // stripping out the native glue library
  app_dummy();
 
  for(uint8_t ui = 0; ui < 100++ui)
  {
    // The function prints to log cat the index of the for loop
    __android_log_print(
      ANDROID_LOG_INFO,
      "UFO Tap Attack",
      " -- CUFOTapAttack::android_main(), ui == %d \n",
      ui);
 
  } // End of for(uint8_t ui = 0; ui < 100; ++ui)
 
// End of void android_main()
cs




헤더를 추가해주시고


__android_log_print("로그 타입", 

   "로그 명", 

   "로그 내용 및 변수 타입", 

   변수);



순서대로 인자를 넣어 쓰시면 됩니다.





추가)

바로 위 코드에서 헤더파일을 <android_native_app_glue.h>, <android/log.h> 두개를 추가했는데


Error:(10, 10) fatal error: 'android_native_app_glue.h' file not found


저는 이런 에러가 납니다ㅜ



그래서 <android_native_app_glue.h>를 지웠더니 잘 실행이 됩니다^^






https://raginggazebo.com/configuring-android-studio-for-native-development/


위의 내용을 참고했습니다.



Comments