Android cmake 多 cpp 引用问题

windydy 2018-08-18 01:45:53
我在 ndk 开发时,多 cpp 在主函数引用时,编译报错;调试了很多天,貌似配置什么的都没有问题啊,菜鸟请教一下:
Build command failed.
Error while executing process /Users/mythwind/Library/Android/sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /Users/mythwind/Learn/Android/democode/TestNdk/app/.externalNativeBuild/cmake/debug/armeabi-v7a --target native-lib}
[1/3] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
[2/3] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/opengl/GLFilter.cpp.o
[3/3] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so
FAILED: : && /Users/mythwind/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=armv7-none-linux-androideabi23 --gcc-toolchain=/Users/mythwind/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/mythwind/Library/Android/sdk/ndk-bundle/sysroot -fPIC -isystem /Users/mythwind/Library/Android/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -mfpu=neon -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -std=c++14 -std=c++11 -fno-rtti -fno-exceptions -Wall -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot /Users/mythwind/Library/Android/sdk/ndk-bundle/platforms/android-23/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--exclude-libs,libunwind.a -L/Users/mythwind/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a -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/armeabi-v7a/libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o CMakeFiles/native-lib.dir/src/main/cpp/opengl/GLFilter.cpp.o -lGLESv3 -llog -landroid -lEGL -llog -latomic -lm "/Users/mythwind/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_static.a" "/Users/mythwind/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++abi.a" "/Users/mythwind/Library/Android/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libunwind.a" "-ldl" && :
/Users/mythwind/Learn/Android/democode/TestNdk/app/src/main/cpp/native-lib.cpp:38: error: undefined reference to 'GLFilter::initFilter(_JNIEnv*, AAssetManager*)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.


CMakeLists.txt 代码:

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

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fno-rtti -fno-exceptions -Wall")
if (${ANDROID_PLATFORM_LEVEL} LESS 12)
message(FATAL_ERROR "OpenGL 2 is not supported before API level 11 \
(currently using ${ANDROID_PLATFORM_LEVEL}).")
return()
elseif (${ANDROID_PLATFORM_LEVEL} LESS 18)
add_definitions("-DDYNAMIC_ES3")
#set(GL3STUB_SRC gl3stub.c)
set(OPENGL_LIB GLESv2)
else ()
set(OPENGL_LIB GLESv3)
endif (${ANDROID_PLATFORM_LEVEL} LESS 12)

file(GLOB native_srcs "src/main/cpp/*.cpp" "src/main/cpp/opengl/*.cpp")

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).
${native_srcs}
)


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

)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
native-lib
${OPENGL_LIB}

# Links the target library to the log library
# included in the NDK.
${log-lib}
android
EGL
log
)



nativ-lib.cpp如下:

extern "C"
JNIEXPORT void JNICALL
Java_com_vincent_mythwind_testndk_ndk_JniInterface_nativeInit(
JNIEnv *env, jobject instance, jobject assetManager) {
AAssetManager *manager = AAssetManager_fromJava(env, assetManager);
if (manager == NULL) {
LOGE("=====jniinterface.AAssetManager is null");
return ;
}
if (mFilter) {
delete mFilter;
mFilter = NULL;
}


LOGE("======nativeInit: filter");
//mFilter->initGL(env, manager);
mFilter->initFilter(env, manager);
LOGE("======nativeInit");

}



GLFilter.h 代码:
#ifndef TESTNDK_GLFILTER_H
#define TESTNDK_GLFILTER_H


extern "C" {
#include <jni.h>
#include "android/asset_manager.h"
#include "android/asset_manager_jni.h"
}

class GLFilter {

public:
void init();
void initFilter(JNIEnv *env, AAssetManager *manager);

};


#endif //TESTNDK_GLFILTER_H


GLFilter.cpp 代码:
#include "GLFilter.h"
#include "../Const.h"

void initFilter(JNIEnv *env, AAssetManager *manager) {

LOGE("=========GLFilter.initFilter=========");

}



...全文
176 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
windydy 2018-08-18
  • 打赏
  • 举报
回复
先自己定一下

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧