[英]Eclipse NDK build error 'Add' was not declared in this scope
我是NDK和JNI的新手,不確定如何解決此錯誤。 我手動將libMathFuncLib.so,mathFuncsLibs.cpp和MathFuncLibs.h文件復制到Eclipse項目。 當我運行此命令'ndk-build'並返回'workspace / test / jni / TestMath.cpp:錯誤:在此范圍內未聲明'Add'。
這是我的文件夾結構:
-test
|__src
|__ExecuteTest.java
|__MainActivity.java
|__jni
|__Android.mk
|__Application.mk
|__TestMath.cpp
|__libs
|__armeabi
|__myLibs
|__armeabi
|__libMathFuncLib.so
|__myNatives
|__MathFuncLibs.cpp
|__MathFuncLib.h
這是MathFuncLib.h文件:
//This is static library example
#ifndef MathFuncLib_INCLUDED
#define MathFuncLib_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif
class MyMathFunc
{
public:
static int Add(int a, int b);
static int Subtract(int a, int b);
static int Multiply(int a, int b);
static double Divide(int a, int b);
};
#ifdef __cplusplus
} // extern "C"
#endif
#endif
這是MathFuncLib.cpp文件:
#include "MathFuncLib.h"
int MyMathFunc::Add(int a, int b)
{
return a + b;
}
int MyMathFunc::Subtract(int a, int b)
{
return a - b;
}
int MyMathFunc::Multiply(int a, int b)
{
return a * b;
}
double MyMathFunc::Divide(int a, int b)
{
return a / b;
}
這是MainActivity.java文件:
package com.example.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;//to use TextView
import android.widget.GridLayout.LayoutParams;//to use LayoutParams
public class MainActivity extends AppCompatActivity {
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int retVal = 0;
ExecuteTest et = new ExecuteTest();
retVal = et.TestAdd();
TextView tv = new TextView(this);
LayoutParams lp = new LayoutParams();
lp.setMargins(150, 50, 200, 0);
tv.setLayoutParams(lp);
tv.setText(String.valueOf(retVal));
setContentView(tv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
這是ExecuteTest.java文件:
package com.example.test;
import android.util.Log;
public class ExecuteTest {
public int ReturnValue(){
return 50;
}
public native int TestAdd();
static
{
System.loadLibrary("MathFuncLib");
System.loadLibrary("Arithmetic");
Log.i ("ExecuteTest", "Shared Libs loaded");
}
}
這是TestMath.cpp文件:
#include <jni.h>
#include <string.h>
#include <android/log.h>
#include "../myNatives/MathFuncLib.h"
extern "C"
{
JNIEXPORT int JNICALL Java_com_example_test_ExecuteTest_TestAdd(JNIEnv *env, jobject obj)
{
__android_log_print(ANDROID_LOG_INFO, "Test", "Inside TestAdd()");
int retVal= Add(50,50);//Add(,) is a method inside MathFuncLib.so file
return retVal;
}
}
這是我的Android.mk文件:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MathFuncLib-prebuilt
LOCAL_SRC_FILES := ../myLibs/armeabi/libMathFuncLib.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := Arithmetic
LOCAL_SRC_FILES := TestMath.cpp
LOCAL_LDLIBS := -llog -lz
include $(BUILD_SHARED_LIBRARY)
這是Application.mk文件:
APP_STL := gnustl_shared
謝謝。
這是正確的表格
Application.mk
APP_ABI := x86 ## -- set to whatever platform you need
APP_PLATFORM := android-9
APP_STL := stlport_static ## -- USE IT
。
至於Android.mk,您將需要2,因為我看到您想使用兩個項目來構建解決方案-
本機(非JNI)庫
第二個本機(JNI)庫,該庫會加載第一個本機,並通過JNI將其提供給JAVA
Application.mk應該重復。
。
Android.mk(本機部分)
include $(CLEAR_VARS)
#
# --------------- C / C++ Project settings ---------------
#
LOCAL_PATH := _path_to__myNatives
# Application/Library Name
LOCAL_MODULE := MathFunc
# Project Source files
LOCAL_SRC_FILES := MathFuncLibs.cpp
# Compiler Flags
LOCAL_CFLAGS := -mfpu=neon
### THIS BIT BELOW IS IMPORTANT -- used so that projects that include this build output will find the correct headers ###
# Location of headers
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/.
#
# ------------------------- .. ----------------------------
#
include $(BUILD_STATIC_LIBRARY)
。
您將使用Eclipse,因此可以在_path_to__myNatives / obj / local / $(TARGET_ARCH_ABI)/libMathFunc.a中找到輸出。 在此示例中,$(TARGET_ARCH_ABI)的值為x86。 需要為您的平台構建一次(在Application.mk中指定)。
。
Android.mk(本機JNI部分)
將“靜態”替換為“共享”(不帶引號的c)
#
# -------------- Dependant Static Libraries Linkage ---------------
#
include $(CLEAR_VARS)
LOCAL_PATH := _path_to__myNatives
LOCAL_MODULE := MathFuncLib ## can be different from the previous module name definition in Android.mk or can be the same
LOCAL_SRC_FILES := $(LOCAL_PATH)/obj/local/$(TARGET_ARCH_ABI)/libMathFunc.a
include $(PREBUILT_STATIC_LIBRARY)
#
# ------------------------- .. ----------------------------
#
#
# --------------- C / C++ Project settings ----------------
#
include $(CLEAR_VARS)
LOCAL_PATH := _path_to___jni
# Application/Library Name
LOCAL_MODULE := Arithmetic
# Project Source files
LOCAL_SRC_FILES := TestMath.cpp
# Compiler Flags
LOCAL_CFLAGS := -mfpu=neon
LOCAL_LDLIBS := -llog -lz
# Static Libraries
LOCAL_STATIC_LIBRARIES := MathFuncLib # must be exactly the same as in THIS .mk file
# Shared Libraries
#LOCAL_SHARED_LIBRARIES := MathFuncLib # must be exactly the same as in THIS .mk file
#
# ------------------------- .. ----------------------------
#
include $(BUILD_SHARED_LIBRARY)
只想分享我的固定。
1)在MyMathFunc.h文件中,我不應該使用“ extern C”,因為C不支持類。 如果要使用“ extern C”,那么我必須刪除類聲明。
2)需要更改Android.mk和Application.mk文件。
Android.mk文件:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MathFuncLib
LOCAL_SRC_FILES := ../myLibs/armeabi/libMathFuncLib.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_SHARED_LIBRARIES := MathFuncLib
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../myNatives/
LOCAL_MODULE := Arithmetic
LOCAL_SRC_FILES := TestMath.cpp
LOCAL_LDLIBS := -llog -lz
#Tell it to build an APK
include $(BUILD_SHARED_LIBRARY)
Application.mk文件:
APP_STL := gnustl_shared
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.