【问题标题】:ndk-build error while Compile the Source Code编译源代码时出现 ndk-build 错误
【发布时间】:2013-07-30 12:43:53
【问题描述】:

我正在学习以下教程 http://taylorpeer.com/hello-world-cpp-android-ndk/

当我在终端中运行 ndk-build 时。我收到以下错误

Compile++ thumb  : hello-jni <= hello-jni.cpp
jni/hello-jni.cpp:4:5: error: stray '\342' in program
jni/hello-jni.cpp:4:5: error: stray '\200' in program
jni/hello-jni.cpp:4:5: error: stray '\234' in program
jni/hello-jni.cpp:4:5: error: stray '\342' in program
jni/hello-jni.cpp:4:5: error: stray '\200' in program
jni/hello-jni.cpp:4:5: error: stray '\235' in program
jni/hello-jni.cpp:9:17: error: stray '\342' in program
jni/hello-jni.cpp:9:17: error: stray '\200' in program
jni/hello-jni.cpp:9:17: error: stray '\234' in program
jni/hello-jni.cpp:9:17: error: stray '\342' in program
jni/hello-jni.cpp:9:17: error: stray '\200' in program
jni/hello-jni.cpp:9:17: error: stray '\235' in program
jni/hello-jni.cpp:4:15: error: 'C' does not name a type
make: *** [obj/local/armeabi/objs/hello-jni/hello-jni.o] Error 1

以下是我的文件列表

在 src 文件夹中找到 java 文件

.

package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

    public class Hellojnicpp extends Activity {
          /** Called when the activity is first created. */
          @Override
          public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                /** Create a TextView and set it to display
                * text loaded from a native method.
                */
                TextView tv = new TextView(this);
                tv.setText(stringFromJNI());
                setContentView(tv);
          }
          /** A native method that is implemented by the
          * ‘hello-jni’ native library, which is packaged
          * with this application.
          */
          public native String stringFromJNI();
          /** Load the native library where the native method
          * is stored.
          */
          static {
                System.loadLibrary("hello-jni");
          }
    }

Android.mk 文件

LOCAL_PATH := $(call my-dir)


include $(CLEAR_VARS)
LOCAL_LDLIBS    := -llog

LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.cpp

include $(BUILD_SHARED_LIBRARY)

.cpp 文件

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

    extern “C” {
          JNIEXPORT jstring JNICALL
          Java_com_example_Hellojnicpp_stringFromJNI
          (JNIEnv *env, jobject obj)
          {
                return env->NewStringUTF(“Hello from C++ over JNI!”);
          }
    }

请帮助我修复过去 72 小时内已经在网上搜索的错误 提前感谢

现在将 "C" 更改为 "C"

后出现以下错误
Compile++ thumb  : hello-jni <= hello-jni.cpp
jni/hello-jni.cpp:9:17: error: stray '\342' in program
jni/hello-jni.cpp:9:17: error: stray '\200' in program
jni/hello-jni.cpp:9:17: error: stray '\234' in program
jni/hello-jni.cpp:9:17: error: stray '\342' in program
jni/hello-jni.cpp:9:17: error: stray '\200' in program
jni/hello-jni.cpp:9:17: error: stray '\235' in program
jni/hello-jni.cpp: In function '_jstring* Java_com_example_Hellojnicpp_stringFromJNI(JNIEnv*, jobject)':
jni/hello-jni.cpp:9:45: error: 'Hello' was not declared in this scope
make: *** [obj/local/armeabi/objs/hello-jni/hello-jni.o] Error 1

【问题讨论】:

  • 您对字符串的引用也是错误的。我已经在我的答案中修复了它。试试看

标签: android-ndk


【解决方案1】:

检查你的“”:

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

    extern "C" {
          JNIEXPORT jstring JNICALL
          Java_com_example_Hellojnicpp_stringFromJNI
          (JNIEnv *env, jobject obj)
          {
                return env->NewStringUTF("Hello from C++ over JNI!");
          }
    }

【讨论】:

【解决方案2】:
extern “C”

应该是

extern "C"

即没有“卷曲”引号。确保您的文本编辑器不会为您“美化”您的直引号。

【讨论】:

  • 将外部“C”更改为“C”,现在得到不同的错误
  • 嗯,让我们看看,你在带花引号的一行中遇到了错误。我告诉过您不要在该行中使用大引号,并且您仍然在 another 行中遇到非常相似的错误,即 also 包含大引号。我是否可以恭敬地建议您可能还没有足够的编程经验来有效地使用 Android NDK?
猜你喜欢
  • 2015-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
  • 2011-09-05
相关资源
最近更新 更多