views:

67

answers:

1

I'm not sure if this is possible, but I would like to create a shared object file and I would like to make it easy to use by having a #define that can be used to dereference the function names.

In libfoo.h

#define FOO_SO_FUNCTION_A    aFunction

In libfoo.so

#include "libfoo/libfoo.h"

extern "C" int FOO_SO_FUNCTION_A( void )
{
    ...
}

In clientfoo

#include "libfoo/libfoo.h"

...
libfoofunc = dlsym( libfoo, MAKE_STRING(FOO_SO_FUNCTION_A) );

My problem is that

#FOO_SO_FUNCTION_A

Will simply change into "FOO_SO_FUNCTION_A" because the preprocessor rightfully only runs once. Is there another way to accomplish this?

+5  A: 

Use this:

#define REALLY_MAKE_STRING(x) #x
#define MAKE_STRING(x) REALLY_MAKE_STRING(x)

Due to some details of the rules when exactly the preprocessors substitutes macros, an extra level of indirection is required.

sth
Note to others looking at this. Don't forget to #include the file that defines the identifier you want to replace with, otherwise you're going to scratch your head wondering why the expansion isn't working and you keep getting the static text.