Search code examples
androidc++android-ndkjava-native-interfaceeclipse-cdt

error: unknown type name 'class' NDK CDT JNI


I'm using Eclipse ADT with: CDT. Along with NDK interfacing with JNI, to c/c++, compiling c/c++ with Cygwin through Eclipse. Everything should be running the latest versions as this was just setup over the last two weeks.

When Building I get the following.

jni/testSocketClass.hpp:33:1: error: unknown type name 'class'
jni/testSocketClass.hpp:33:17: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
jni/ndkfoo.c:13:1: error: unknown type name 'mYNewClass'

JNI C file

#include <string.h>
#include <jni.h>
#include "testSocketClassWrapper.hpp"

void *m_GBLpmyCSocket;

ndkfoo.c
jstring Java_com_example_hydrobedcontrol1_MainActivity_inintNativeClass(JNIEnv * env, jobject object){

  m_GBLpmyCSocket = MyClass_create();
  MyClass_sendCommandToSerialDevice(m_GBLpmyCSocket, 0, 0, 0);
 return (*env)->NewStringUTF(env, "started");
}

Class Wrapper .hpp

 //file testSocketClassWrapper.hpp
#ifndef _MY_SOCKETCLASS_WRAPPER_H
#define _MY_SOCKETCLASS_WRAPPER_H

#include"testSocketClass.hpp"//<<<<<<<<<<<<<<<<<<<<<Wrong inclusion


#ifdef __cplusplus

extern "C" void* MyClass_create() {
return new mYNewClass;
}
extern "C" void MyClass_release(void* myclass) {
delete static_cast<mYNewClass*>(myclass);
}
extern "C" void MyClass_sendCommandToSerialDevice(void* myclass, int cmd, int params,  int id) {
static_cast<mYNewClass*>(myclass)->sendCommandToSerialDevice(cmd,params,id);
}

#endif
#endif /* _MY_SOCKETCLASS_WRAPPER_H_INCLUDED */

Class wrapper .cpp

//file testSocketClassWrapper.cpp
#include "testSocketClassWrapper.hpp"

Class .h

// file testSocketClass.hpp
class mYNewClass{///////////////////////ERROR HERE////////////////////////////////

//public:

void sendCommandToSerialDevice(int Command, int Parameters, int DeviceID);
//int sockfd;

 };

Class .cpp

// file testSocketClass.cpp
#include "testSocketClass.hpp"

void mYNewClass::sendCommandToSerialDevice(int Command, int Parameters, int DeviceID){

char testc[100];
sprintf(testc, "%d, %d, %d", Command, Parameters, DeviceID);

}

I've read countless questions on this topic and have had quite a few issues getting this far with Eclipse configurations too. But from what I've pulled together from other questions on this topic, I have hit a wall and do not know how to proceed at this point. Suggestions?

EDIT BELOW - ANSWER

After review with a colleague(sometimes a second pair of eyes helps bring things out), we located my error. See the line labeled improper inclusion in the Class wrapper .hpp. That header should be relocated as follows in the Class wrapper .cpp NOTE: the functions where also moved to the .cpp and the .hpp is now empty.

//file testSocketClassWrapper.cpp
#include "testSocketClassWrapper.hpp"
#include "testSocketClass.hpp"

extern "C" void* MyClass_create() {
  return new mYNewClass;
}
extern "C" void MyClass_release(void* myclass) {
  delete static_cast<mYNewClass*>(myclass);
}
extern "C" void MyClass_sendCommandToSerialDevice(void* myclass, int cmd, int params, int id) {
  static_cast<mYNewClass*>(myclass)->sendCommandToSerialDevice(cmd,params,id);
}

Also for completeness as this has been no walk in the park, are the MK files.

Android.mk
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

# Here we give our module name and source file(s)
LOCAL_MODULE    := ndkfoo
LOCAL_SRC_FILES := ndkfoo.c testSocketClassWrapper.cpp testSocketClass.cpp

include $(BUILD_SHARED_LIBRARY)    


Application.mk
APP_STL:=stlport_static

Thank you for the prompt reply krsteeve. And yet another way to do this. Will keep it in mind.


Solution

  • testSockedClass.hpp is being included from a .c file, so it's being compiled with a C-compiler. class doesn't exist in C. Change ndkfoo.c to a .cpp file.

    You will have to format things a bit differently in C++:

    extern "C"
    {
        jstring Java_com_example_hydrobedcontrol1_MainActivity_inintNativeClass(JNIEnv * env, jobject object);
    }
    
    jstring Java_com_example_hydrobedcontrol1_MainActivity_inintNativeClass(JNIEnv * env, jobject object){
    
        m_GBLpmyCSocket = MyClass_create();
        MyClass_sendCommandToSerialDevice(m_GBLpmyCSocket, 0, 0, 0);
        return env->NewStringUTF("started");
    }
    

    Note the form of the NewStringUTF call in C++ vs C.