error: pasting "Java_com_my_packagename_myClass_myMethod" and "(" does not give a valid preprocessing token

StackOverflow https://stackoverflow.com/questions/18542088

  •  26-06-2022
  •  | 
  •  

Question

I'm writing a macro to make life easier by generating JNI method names, using the preprocessor:

#define JNI_WRAPPER_METHOD (className, methodName, returnValue, PARAMS) \
JNIEXPORT returnValue JNICALL Java_com_my_packagename_className_methodName\
(JNIEnv *env, jobject obj, BOOST_PP_SEQ_ENUM(PARAMS))

so, ideally this:

JNI_WRAPPER_METHOD(myClass, myMethod, jint, (jint myInt)(jstring myString))

would translate to this:

JNIEXPORT jint JNICALL Java_com_my_packagename_myClass_myMethod(JNIEnv *env, jobject obj, jint myInt, jstring myString)

However, the compiler throws the following error when I attempt to use my macro:

error: pasting "Java_com_my_packagename_myClass_myMethod" and "(" does not give a valid preprocessing token

Does anyone know why the macro fails?

Was it helpful?

Solution

You need to concatenate strings like:

 Java_com_my_packagename_ ## className ## _ ## methodName

Also don't leave a space in the definition:

#define JNI_WRAPPER_METHOD(className, methodName, returnValue, PARAMS) 

All in all, the following works fine:

#define JNI_WRAPPER_METHOD(className, methodName, returnValue, PARAMS) \
        JNIEXPORT returnValue JNICALL                                  \
        Java_com_my_packagename_ ## className ## _ ## methodName       \
        (JNIEnv *env, jobject obj, BOOST_PP_SEQ_ENUM(PARAMS))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top