包含本地头文件时未定义对“ XXX”的引用

时间:2019-01-30 10:27:42

标签: android cmake android-ndk

我正在使用ndk和cmake构建我的android项目。我创建一个.h文件,并将其导入其他.cpp文件。但是构建失败。

undefined reference when using local header file 我尝试Erik.E的答案,但没有用

MY CMakeLists.txt文件

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})


native-lib.cpp的一些代码

#include "constant.h"

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

我的头文件

#include <string>

using namespace std;

#ifdef __cplusplus
extern "C" {
#endif

class Constant {
public:
    static string BASE_PATH;
};

#ifdef __cplusplus
}
#endif

我的文件结构

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

错误消息

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.

0 个答案:

没有答案