Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing a name of a function using define macros in cpp correctly

I'm using Eclipse + Qualcomm libraries (in cpp) + Android SDK on Ubuntu. My application runs fine. If I change some code in the qualcomm libraries, it compiles and works correctly.

The problem is: I have changed the name of the project, and I have to change some code in cpp (The name of the function), if I don't do it, I get a Java.lang.UNSATISFIEDLINKERROR.

That's because all the functions have the name as the Android package like this:

Java_org_myproject_marker_MainActivity_onQCARInitializedNative(JNIEnv *, jobject)

Then I define a macro like this:

#define MAIN_ACTIVITY_PREFIX org_myproject_marker_MainActivity
#define VISUALIZER_PREFIX org_myproject_marker_Visualizer

And I change all the correct functions by:

Java_MAIN_ACTIVITY_PREFIX_onQCARInitializedNative(JNIEnv *, jobject)

but I am still getting the Java.lang.UNSATISFIEDLINKERROR exception.

It works if I do it without the #define macro (and write all the lines), but I want to save the cpp code with a top define that changes everything automatically if I need to use it in other projects.

I have read this tutorial. Can't I replace a text inside another text or something like that?

like image 326
vgonisanz Avatar asked Jan 09 '12 08:01

vgonisanz


People also ask

What is a macro in C++?

A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive. Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro.

Does the preprocessor support text macro replacement?

The preprocessor supports text macro replacement. Function-like text macro replacement is also supported. The #define directives define the identifier as macro, that is instruct the compiler to replace all successive occurrences of identifier with replacement-list, which can be optionally additionally processed.

What is the difference between a function call and a macro?

Also note that macro replacement commonly results in multiply evaluation of the arguments, whereas a function call does not, and hence macros replicate side effects contain inside the argument, such as ++ and other function calls. Function calls also convert arguments to parameter types, whereas macros do not.

How to expand a CPP macro in the middle of identifier?

Indeed, a CPP macro wont be expanded in the middle of an identifier. Try with That gives you a macro that will prepend Java_org_myproject_marker_MainActivity to the function name you pass it. Use it as: MAIN_ACTIVITY_PREFIX (_onQCARInitializedNative) (JNIEnv *, jobject) { ...


1 Answers

you are looking for string concatenation, like this:

#define MAIN_ACTIVITY_PREFIX(n) Java_org_myproject_marker_MainActivity##n

and then use it like this:

MAIN_ACTIVITY_PREFIX(_onQCARInitializedNative)(JNIEnv *, jobject)
like image 153
PeterT Avatar answered Oct 24 '22 01:10

PeterT