android - Android NDK 项目中的 undefined reference 错误

标签 android c++ android-ndk cmake

我想将 C++ 文件导入到我的 android 项目中。首先,我使用 android studio 创建了一个支持 C++ 的项目。它提供骨架项目结构。我使用 add_library 方法通过 CMakeLists.text 文件加载了我的文件。我可以成功加载我的 c++ 文件并能够在项目 View 中查看。当我试图从我的 native-lib.cpp 中的 mClient.cpp 调用一个方法时,我得到了

undefined reference to "hello()"

错误。由于我没有看到任何 IDE 警告,我可以说我的文件已成功加载。但它在编译时不起作用。我还尝试通过 cmake 使用 add_executable 加载,但它也会产生相同的错误。请问我可以知道我的配置和设置中缺少什么吗?我是 NDK 和 CMake 的新手。 感谢您的宝贵时间。

mClinet.cpp

#include "mClient.h"

static int hello(){
    return 1;
}

客户端.h

#ifdef __cplusplus
extern "C" {
#endif

static int hello();

#ifdef __cplusplus
}
#endif
#endif

这是我的 CMake.text 文件。

# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.

cmake_minimum_required(VERSION 3.4.1)

# 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 it for you.
# Gradle automatically packages shared libraries with your APK.

# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -UNDEBUG")

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             # Associated headers in the same location as their source
             # file are automatically included.
             src/main/cpp/native-lib.cpp
             src/main/cpp/mClient.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included 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.

find_library( # Sets the name of the path variable.
              log-lib

              # 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 the
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries(native-lib android log)

我想从我的 native-lib.cpp 调用 hello()

#include <jni.h>
#include <string>
#include "mClient.h"

extern "C"
jstring
Java_com_zawmyohtet_hellondk_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {

    int myInt = hello();

    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

最佳答案

删除staticstatic 意味着函数在其翻译单元之外将不可访问。

关于android - Android NDK 项目中的 undefined reference 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39667823/

相关文章:

java - 缓存和线程问题

c++ - 为什么内联在这里不起作用?

c++ - if constexpr 中的 requires 子句问题

android - java.lang.NullPointerException - 使用 opencv 和 android

android - 如何在 cygwin for windows 中使用 ndk-build 构建 libav?

java - Android: native 代码如何与java代码交互?

android - 传输数据 USB

android - 滚动横幅 - 实现它的最佳方式是什么?

android - 在 travis yml 文件上配置 gradle.properties android.enableAapt=false

c++ - reinterpret_cast 并与 CV_MAT_ELEM 一起使用