预处理器语法,使用#define作为标识符(函数名称)和字符串

use*_*522 2 c++ c-preprocessor

我不确定这是否可行,但我想创建一个共享对象文件,我希望通过使用#define可以用来取消引用函数名称来使其易于使用.

libfoo.h中

#define FOO_SO_FUNCTION_A    aFunction
Run Code Online (Sandbox Code Playgroud)

libfoo.so中

#include "libfoo/libfoo.h"

extern "C" int FOO_SO_FUNCTION_A( void )
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

clientfoo中

#include "libfoo/libfoo.h"

...
libfoofunc = dlsym( libfoo, MAKE_STRING(FOO_SO_FUNCTION_A) );
Run Code Online (Sandbox Code Playgroud)

我的问题是

#FOO_SO_FUNCTION_A
Run Code Online (Sandbox Code Playgroud)

将简单地改为"FOO_SO_FUNCTION_A",因为预处理器只运行一次.还有另一种方法来实现这一目标吗?

sth*_*sth 5

用这个:

#define REALLY_MAKE_STRING(x) #x
#define MAKE_STRING(x) REALLY_MAKE_STRING(x)
Run Code Online (Sandbox Code Playgroud)

由于规则的一些细节,当预处理器完全替换宏时,需要额外的间接级别.