0

I'm using ndk and cmake to build my android project. I create a .h file and import it in other .cpp file. But build failed.

undefined reference when using local header file I try Erik.E's answer but no use

MY CMakeLists.txt file

cmake_minimum_required(VERSION 3.4.1)

set(C_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp)

message(${CMAKE_CURRENT_SOURCE_DIR})

add_library(native-lib SHARED ${C_DIR}/native-lib.cpp)

add_library(android-utils SHARED ${C_DIR}/android-utils.cpp)

find_library(log-lib log)

find_library(android android)

target_include_directories(native-lib PRIVATE ${C_DIR}/headers)

target_link_libraries(
        android-utils
        ${log-lib}
        ${android}
)

target_link_libraries( # Specifies the target library.
        native-lib
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib}
        ${android})


Some code of native-lib.cpp

#include "constant.h"

if(Constant::BASE_PATH.empty()){
   Constant::BASE_PATH = jstring2charArray(env, dv);
}

MY header file

#include <string>

using namespace std;

#ifdef __cplusplus
extern "C" {
#endif

class Constant {
public:
    static string BASE_PATH;
};

#ifdef __cplusplus
}
#endif

My File Struct

cpp
---native-lib.cpp
---android-utils.cpp
---headers 
   ---constant.h

ERROR MESSAGE

Build command failed.
Error while executing process C:\Users\c\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build D:\projects\AndroidstudioProjects\Asr\app\.externalNativeBuild\cmake\debug\arm64-v8a --target native-lib}
[1/1] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\arm64-v8a\libnative-lib.so
FAILED: cmd.exe /C "cd . && C:\Users\c\AppData\Local\Android\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe  --target=aarch64-none-linux-android21 --gcc-toolchain=C:/Users/c/AppData/Local/Android/Sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=C:/Users/c/AppData/Local/Android/Sdk/ndk-bundle/sysroot -fPIC -isystem C:/Users/c/AppData/Local/Android/Sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -frtti -fexceptions -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot C:/Users/c/AppData/Local/Android/Sdk/ndk-bundle/platforms/android-21/arch-arm64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -LC:/Users/c/AppData/Local/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a -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\arm64-v8a\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o  -llog -landroid -latomic -lm "C:/Users/c/AppData/Local/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_static.a" "C:/Users/c/AppData/Local/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++abi.a" && cd ."
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: In function `Java_com_weechan_asr_jutils_Utils_get':
D:\projects\AndroidstudioProjects\Asr\app\src\main\cpp/native-lib.cpp:74: undefined reference to `Constant::BASE_PATH'
D:\projects\AndroidstudioProjects\Asr\app\src\main\cpp/native-lib.cpp:74: undefined reference to `Constant::BASE_PATH'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
weechan
  • 87
  • 9

0 Answers0