1
votes

I have 3 class,native-lib.cpp, tracker.cpp and tracker.hpp, However, I am getting an error during build if I use the isTracking function from tracker.cpp in navtive-lib.cpp.

  • What went wrong: Execution failed for task ':app:externalNativeBuildDebug'.

    Build command failed. Error while executing process /home/user/Android/Sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /home/user/AndroidStudioProjects/project/app/.externalNativeBuild/cmake/debug/x86 --target tracker} [1/2] Building CXX object CMakeFiles/tracker.dir/src/main/cpp/native-lib.cpp.o [2/2] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/x86/libtracker.so FAILED: : && /home/user/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=i686-none-linux-android --gcc-toolchain=/home/user/Android/Sdk/ndk-bundle/toolchains/x86-4.9/prebuilt/linux-x86_64 --sysroot=/home/user/Android/Sdk/ndk-bundle/sysroot -fPIC -isystem /home/user/Android/Sdk/ndk-bundle/sysroot/usr/include/i686-linux-android -D__ANDROID_API__=23 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mstackrealign -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a --sysroot /home/user/Android/Sdk/ndk-bundle/platforms/android-23/arch-x86 -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,libtracker.so -o ../../../../build/intermediates/cmake/debug/obj/x86/libtracker.so CMakeFiles/tracker.dir/src/main/cpp/tracker.cpp.o CMakeFiles/tracker.dir/src/main/cpp/native-lib.cpp.o /home/user/Android/Sdk/ndk-bundle/platforms/android-23/arch-x86/usr/lib/liblog.so ../../../../src/main/jniLibs/x86/libopencv_java3.so -lm "/home/user/Android/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/x86/libgnustl_static.a" && : /home/user/AndroidStudioProjects/project/app/src/main/cpp/native-lib.cpp:28: error: undefined reference to 'Tracker::isTracking()' clang++:

error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed.

My CMakeLists

set (pathToOpenCv /home/user/OpenCV-android-sdk3.2) set (pathToProject /home/user/AndroidStudioProjects/project)

cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_VERBOSE_MAKEFILE on)

include_directories(${pathToOpenCv}/sdk/native/jni/include)

FILE(GLOB_RECURSE cppfiles "/home/user/AndroidStudioProjects/project/app/src/main/cpp/*.cpp")

add_library(lib_opencv SHARED IMPORTED)

add_library(tracker SHARED ${cppfiles})

set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathToProject}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)

find_library( log-lib

              log ) target_link_libraries(
                       tracker

                       ${log-lib}

                       lib_opencv
                       )

This is my native-lib.cpp

#include <jni.h>
#include <string>
#include "tracker.hpp"

using namespace cv;

Tracker tracker;

extern "C"
JNIEXPORT jboolean JNICALL
Java_com_example_user_project_Tracker_isTrackingC(JNIEnv *env, jboolean boolean) {
    boolean = tracker.isTracking();
    return boolean;
}

What could be the reason, CMake cannot find my function from tracker.cpp in native-lib? I think I linked all my cpp together by using add_library(tracker SHARED ${cppfiles}) and target_link_libraries in CMakeLists.

2
I believe it is because tracker is never initialized.Zamrony P. Juhara
Have you tried replacing the FILE(GLOB_RECURSE) line with a list of your source files? You don't have to put the full path, you can just use the filename as long as CMakeLists.txt is in the same folder as the .cpp files. It looks suspiciously like tracker.cpp is not getting compiled. Also can you post tracker.cpp and tracker.hpp?MultipleMonomials
Don't know about clang, but if it behaves like gcc your object files are in the wrong order.user2543253
Thanks, it works now. I did not initialize the isTracking() function inside Tracker.cpp, but only Tracker.hpp. I thought that was my problem inside CMakelists, and did not check my cpp yestarday.chan3600

2 Answers

1
votes

"Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/x86/libtracker.so FAILED" that is the key point. "--target=i686-none-linux-android " maybe your project compile toolchains should use 32bit

1
votes

Thanks all. After reading the comments from Zamrony and MultipleMonomials, I double checked the Tracker.cpp and Tracker.hpp again. I found that I declared isTracking() on hpp, but did not define it inside .cpp file. After implementing the function in .cpp file, it worked fine.