-1

I am following the following tutorial: http://taylorpeer.com/hello-world-cpp-android-ndk/

When I run ndk-build in the terminal, I get the following errors:

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

Following is the list of my files:

Java file found in the src folder

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 file

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 file

#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!”);
    }
}

How can I fix the errors?

I am now getting the following error after changing "C" to "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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sahil Khan
  • 31
  • 8
  • your quote for string is also wrong. I've fixed it in my answer. try it – yushulx Jul 30 '13 at 13:26
  • This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Apr 28 '23 at 16:02
  • For this particular error: 342 200 234 (octal) → 0xE2 0x80 0x9C (hexadecimal) → UTF-8 sequence for Unicode code point U+201C ([LEFT DOUBLE QUOTATION MARK](https://www.charset.org/utf-8/9)). And 342 200 235 (octal) → 0xE2 0x80 0x9D (hexadecimal) → UTF-8 sequence for Unicode code point U+201D ([RIGHT DOUBLE QUOTATION MARK](https://www.charset.org/utf-8/9)). They can be searched for (and replaced) by the regular expression `\x{201C}|\x{201D}` in any modern text editor (the notation is different in Visual Studio Code (and probably others): `\u201C|\u201D`). – Peter Mortensen Apr 28 '23 at 16:02
  • The *taylorpeer.com* link is (effectively) broken. It times out. But it was probably ***copying source code from that web page*** that caused this problem. – Peter Mortensen Apr 28 '23 at 16:24

2 Answers2

2

Check your "":

#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!");
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yushulx
  • 11,695
  • 8
  • 37
  • 64
  • can you help me out in my problem which is similar to this. you can find it here https://stackoverflow.com/questions/51100111/how-to-resolve-android-ndk-build-command-faild – Nitish Patel Jul 02 '18 at 13:16
1
extern “C”

should be

extern "C"

i.e., no "curly" quotes. Make sure your text editor does not "beautify" your straight quotes for you.

wolfgang
  • 4,883
  • 22
  • 27
  • changed extern “C” to "C" and now getting the different error – Sahil Khan Jul 30 '13 at 12:58
  • Hmmm, let's see, you got an errors in a line with curly quotes. I told you not to use curly quotes in that line, and you are still getting a very similar error in *another* line that *also* contains curly quotes. Might I respectfully suggest that you might not yet have enough programming experience to effectively use the Android NDK? – wolfgang Jul 31 '13 at 17:30