lizihui 发表于 2009-4-26 11:04:02

请教傻孩子关于条件编译

条件编译能否可以这样写?
如:

#define NUMBER 3

void main()
{
    #if NUMBER==1
    {
      ;
    }
    #endif
    #if NUMBER==2
    {
      ;
    }
    #endif
    #if NUMBER==3
    {
      ;
    }
    #endif
    #if NUMBER==4
    {
      ;
    }
    #endif
    #if NUMBER==5
    {
      ;
    }
    #endif
}

yajira 发表于 2009-4-26 11:14:40

连括号都不需要吧

lizihui 发表于 2009-4-26 11:23:26

我如果加上
    #if NUMBER==3
    {
      ;
    }
就报错
!E E:(129):unrecognized statement
!E E:(129): syntax error; found `(' expecting `;'
!W E:130): expression with no effect elided
!E E:(130): syntax error; found `{' expecting `;'
Done: there are error(s). Exit code: 1

Gorgon_Meducer 发表于 2009-4-26 23:10:14

129和130行是哪一行?

lizihui 发表于 2009-4-30 16:10:47

暂时不管报错了,首先这种写法有没有问题,ICC允许这样写吗?

aozima 发表于 2009-4-30 16:52:32

这和ICC无关吧? 都支持这么写的...

Gorgon_Meducer 发表于 2009-5-1 01:03:08

to 【4楼】 lizihui
   没有问题,如果有错误,请检查是否代码的前面有错误,或者含有非法的中文字符……

zxq6 发表于 2009-5-1 02:33:44

这种写法是可以的,我刚刚试试了下,
#define NUMBER 3

int main(void)
{
    #if NUMBER==1
    {
      ;
    }
    #elseif NUMBER==2
    {
      ;
    }
    #elseif NUMBER==3
    {
      ;
    }
    #elseif NUMBER==4
    {
      ;
    }
        #elseif NUMBER==5
    {
      ;
    }
    #endif
        return 0;
}
编译结果:
CPU: ATmega16
Program memory usage: 158 bytes, 0.96% full.
Eeprom memory usage: 0 bytes, 0.00% full.
Global variables usage: 0 bytes

atmantest.elf - 0 error(s), 0 warning(s)


编译器为atmanavr

lizihui 发表于 2009-5-1 14:22:57

谢谢小马哥哈!我本想把有些函数写成库函数,但对条件编译还不精通,而且感觉功力还不够,暂时放弃了,现在还是针对固定的硬件写程序

zxq6 发表于 2009-5-4 01:15:13

楼上,有空可以研究下usbasp的源码,非常受益,呵呵。

Tangtk0314 发表于 2010-8-25 14:06:16

这种代码在不通平台移植不错。学习了。
页: [1]
查看完整版本: 请教傻孩子关于条件编译