How to create a c++ class (.h and .cpp) in android studio? -
i learning use android ndk , try create native c++ class (.h , .cpp). followed official tutorial (https://developer.android.com/studio/projects/add-native-code.html) achieve this. managed create simple c++ class , call java, no problem.
now want create own c++ class (let's hellowworld class) constructor nothing. this, right click on cpp folder contain working jni wrapper.
i create class , create default constructor , call jni function crashed during compilation:
error:failure: build failed exception.
what went wrong: execution failed task ':app:externalnativebuilddebug'.
build command failed. error while executing 'c:\users\lucien.moor\appdata\local\android\sdk\cmake\3.6.3155560\bin\cmake.exe' arguments {--build c:\users\lucien.moor\desktop\tmp\myapplication2\app.externalnativebuild\cmake\debug\mips64 --target native-lib} [1/2] building cxx object cmakefiles/native-lib.dir/src/main/cpp/native-lib.cpp.o [2/2] linking cxx shared library ........\build\intermediates\cmake\debug\obj\mips64\libnative-lib.so failed: cmd.exe /c "cd . && c:\users\lucien.moor\appdata\local\android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=mips64el-none-linux-android --gcc-toolchain=c:/users/lucien.moor/appdata/local/android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=c:/users/lucien.moor/appdata/local/android/sdk/ndk-bundle/platforms/android-21/arch-mips64 -fpic -g -dandroid -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -wa,--noexecstack -wformat -werror=format-security -g -dandroid -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -wa,--noexecstack -wformat -werror=format-security -o0 -fno-limit-debug-info -o0 -fno-limit-debug-info -wl,--build-id -wl,--warn-shared-textrel -wl,--fatal-warnings -wl,--no-undefined -wl,-z,noexecstack -qunused-arguments -wl,-z,relro -wl,-z,now -wl,--build-id -wl,--warn-shared-textrel -wl,--fatal-warnings -wl,--no-undefined -wl,-z,noexecstack -qunused-arguments -wl,-z,relro -wl,-z,now -shared -wl,-soname,libnative-lib.so -o ........\build\intermediates\cmake\debug\obj\mips64\libnative-lib.so cmakefiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -lm "c:/users/lucien.moor/appdata/local/android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/mips64/libgnustl_static.a" && cd ." cmakefiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: in function
java_he_1arc_myapplication2_mainactivity_stringfromjni': c:\users\lucien.moor\desktop\tmp\myapplication2\app\src\main\cpp/native-lib.cpp:10: undefined reference to
helloworld::helloworld()' clang++.exe: error: linker command failed exit code 1 (use -v see invocation) ninja: build stopped: subcommand failed.try: run --stacktrace option stack trace. run --info or --debug option more log output.
i think there problem while linking .h , .cpp file. when try implement constructor inline, works fine. doesn't find .cpp implementation.
here jni native-lib.cpp file :
#include <jni.h> #include <string> #include "helloworld.h" extern "c" jstring java_he_1arc_myapplication2_mainactivity_stringfromjni( jnienv *env, jobject /* */) { helloworld t; std::string hello = "hello c++"; return env->newstringutf(hello.c_str()); }
here helloworld.h:
#ifndef myapplication2_helloworld_h #define myapplication2_helloworld_h class helloworld { public: helloworld(); }; #endif //myapplication2_helloworld_h
and here helloworld.cpp
#include "helloworld.h" helloworld::helloworld() { }
when open file, android studio tells me "this file not part of project. please include in appropriate build file (build.gradle, cmakelists.txt or android.mk etc.) , sync project."
so, how link these lovely .h , .cpp together?
i found solution!
as message suggested, had add file inside cmakelists.txt. headers file not required .cpp file has added in cmakelists.txt
to allow linker find implementation file, hust had add
src/main/cpp/helloworld.cpp in cmakelists.txt
here full cmakelists file:
cmake_minimum_required(version 3.4.1) add_library( # sets name of library. native-lib # sets library shared library. shared # provides relative path source file(s). # associated headers in same location source # file automatically included. src/main/cpp/native-lib.cpp src/main/cpp/helloworld.cpp) find_library( # sets name of path variable. log-lib # specifies name of ndk library # want cmake locate. log ) target_link_libraries( # specifies target library. native-lib # links target library log library # included in ndk. ${log-lib} )
Comments
Post a Comment