仔细想了一下,楼主位的问题无解
对于#define XXX YYY
编译器在预处理时,只关会将搜索到的XXX,替换成YYY,也就是说在扫描代码的时候,遇到#define时,会马上把后面标识存到宏表里面
楼主位想要动态生成这个宏表,必须要在预处理之前,也就是说靠C自身的预处理机身完成不了这个任务
楼主为何不换个思路,这样来写你的代码
# include <boost/preprocessor/array.hpp>
#define ARRAY_SDA (2, (A, 1))
#define ARRAY_SCL (2, (A, 1))
#define Port(x) BOOST_PP_CAT(GPIO, BOOST_PP_ARRAY_ELEM(0, BOOST_PP_CAT(ARRAY_, x) ))
#define Pin(x) BOOST_PP_CAT(GPIO_Pin_, BOOST_PP_ARRAY_ELEM(1, BOOST_PP_CAT(ARRAY_, x) ))
#define Clr(x) GPIO_ResetBits( Port(x), Pin(x) )
#define Set(x) GPIO_SetBits( Port(x), Pin(x) )
#define Get(x) GPIO_ReadBit( Port(x), Pin(x) )
void test(void)
{
Clr(SDA);
Set(SDA);
Get(SCL);
}
展开后变成
void test(void)
{
GPIO_ResetBits( GPIOA, GPIO_Pin_1 );
GPIO_SetBits( GPIOA, GPIO_Pin_1 );
GPIO_ReadBit( GPIOA, GPIO_Pin_1 );
}
|