dev-logs

[안드로이드] Jni 빌드 에러 (Cmake) 본문

공부/에러해결

[안드로이드] Jni 빌드 에러 (Cmake)

두룹두두 2018. 4. 9. 22:27

​@에러

Error:(150) undefined reference to '함수이름'

위와 같은 에러가 여러 함수 에서 발생했습니다.
​@해결1

에러가 발생한 함수를 func1,
func1 이 정의된 c파일을 cSource.c
func1 이 선언된 h파일을 header.h
func1을 호출하는 cpp파일을 main.cpp

라고 해보겠습니다.

main.cpp 에서 header.h를 include 할때

#ifdef __cplusplus
extern “C” {
#endif

#include “header.h”

#ifdef __cplusplus
}
#endif


처럼 되어있는지 확인해보세요.

C코드를 include 하는거라 c표시를 해야 하는것 같습니다.


@해결2

위 방법으로 해결이 안됐다면, CmakeLists.txt 를 보셔야 합니다.

—————————————-CmakeLists.txt

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

​#opencv sdk 경로
set(pathOPENCV C:/OpenCV-android-sdk)

​#안드로이드 프로젝트 경로
set(pathPROJECT D:/Project/openCV331)

​#opencv 모듈 경로
set(pathLIBOPENCV_JAVA ${pathPROJECT}/app/src/main/JniLibs/${ANDROID_ABI}/libopencv_java3.so)


set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")


​#src/main/cpp 경로 파일을 포함(라이브러리2에필요)
file(GLOB Library_SOURCES src/main/cpp/*.cpp)
file(GLOB Library_HEADERS src/main/cpp/*.h)
file(GLOB Library_SOURCES_C src/main/cpp/*.c)


include_directories(${pathOPENCV}/sdk/native/jni/include)
include_directories(${pathPROJECT}/src/main/cpp)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

#생성할 라이브러리1
add_library( # Sets the name of the library.
native1-lib

# Sets the library as a shared library.
SHARED

# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )

​#생성할 라이브러리2
add_library(
native2-lib

SHARED

${Library_SOURCES} ${Library_HEADERS} ${Library_SOURCES_C})

add_library( lib_opencv SHARED IMPORTED )




set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathLIBOPENCV_JAVA})

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.


# Specifies the name of the NDK library that
# you want CMake to locate.
log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

​target_link_libraries( # Specifies the target library.
native1-lib

lib_opencv

# Links the target library to the log library
# included in the NDK.
)


​target_link_libraries( # Specifies the target library.
native2-lib

lib_opencv

)


——————————————————-
쓸데없이 예시가 길었네요ㅠ
핵심은 내가 만들어야할 native 라이브러리가 2개이면 ​target_link_libraries 도 따로따로 2번 해줘야 한다는 것입니다.


혹시나 저와 같은 에러가 생긴 분들에게 도움이 됐으면 좋겠습니다!

Comments