0

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?

MM.
  • 4,224
  • 5
  • 37
  • 74
  • Please learn about the `##` preprocessor concatenation operator. Or, if you want anything remotely sane, just avoid the preprocessor altogether. – syam Aug 30 '13 at 22:09
  • I'm surprised it complained about *pasting* `"Java_com_my_packagename_myClass_myMethod"` and `"("`, since you don't have a token-pasting operator in your macro definition. Are you sure that's the exact code that produced that message? – Keith Thompson Aug 30 '13 at 23:51
  • Why? Why not use 'javap' as the designers intended? – user207421 Aug 31 '13 at 00:34

1 Answers1

2

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))
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084