Eclipse NDK构建错误'添加'在这方面没有申明

时间:2016-02-12 19:01:57

标签: android android-ndk

我是NDK和JNI的新手,不知道如何修复此错误。我手动将libMathFuncLib.so,mathFuncsLibs.cpp和MathFuncLibs.h文件复制到Eclipse项目。当我运行这个命令时,ndk-build'然后我回来了工作区/ test / jni / TestMath.cpp:错误:'添加'未在此范围内声明。'

这是我的文件夹结构:

-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

感谢。

2 个答案:

答案 0 :(得分:0)

这是

的正确形式

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 部分)

替换&#34;静态&#34;与&#34;共享&#34; (没有引用的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 :(得分:0)

只想分享我的固定。

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