[英]undefined reference error in Android NDK project
I want to import c++ files to my android project. 我想将c ++文件导入我的android项目。 Firstly, I created a project with C++ support with android studio.
首先,我使用android studio创建了一个具有C ++支持的项目。 It provide skeleton project structure.
它提供了骨架项目结构。 I loaded my file via CMakeLists.text file with add_library method.
我使用add_library方法通过CMakeLists.text文件加载了文件。 I can successfully load my c++ file and able to see in Project view.
我可以成功加载我的c ++文件,并且可以在“项目”视图中看到。 When I am trying to call a method from mClient.cpp inside my native-lib.cpp, I got
当我尝试从native-lib.cpp中的mClient.cpp调用方法时,我得到了
undefined reference to "hello()"
未定义对“ hello()”的引用
error. 错误。 As I didn't see any IDE warning, I can say my file is successfully loaded.
由于没有看到任何IDE警告,因此可以说我的文件已成功加载。 But it doesn't work at compile time.
但是它在编译时不起作用。 I also tried to load with add_executable via cmake but it also produce the same error.
我也尝试通过cmake加载add_executable,但它也会产生相同的错误。 Please may I know what is missing in my configuration and setting?
请问我知道我的配置和设置中缺少什么吗? I am new to NDK and CMake.
我是NDK和CMake的新手。 Thanks for you time.
谢谢您的时间。
mClinet.cpp mClinet.cpp
#include "mClient.h"
static int hello(){
return 1;
}
mClient.h mClient.h
#ifdef __cplusplus
extern "C" {
#endif
static int hello();
#ifdef __cplusplus
}
#endif
#endif
Here is my CMake.text file. 这是我的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)
I want to call hello() from my native-lib.cpp 我想从我的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());
}
Remove static
. 去除
static
。 static
means that a function will not be accessible outside its translation unit. static
表示函数无法在其翻译单元外部访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.