开发者

Preprocessor syntax to use an #define as an identifier (function name) and a string

开发者 https://www.devze.com 2022-12-21 02:12 出处:网络
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.

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消