linux内核常用宏实例【一】 /* Force a compilation error if condition is true */ #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]) 如果条件为真则引起一个编译时错误。 使用实例:static int __init spidev_init(void) { int status; /* Claim our 256 reserved device numbers. Then register a class * that will key udev/mdev to add/remove /dev nodes. Last, register * the driver which manages those device numbers. */ BUILD_BUG_ON(N_SPI_MINORS > 256);//如果条件为真,则引起一个编译错误 status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);//注册驱动 if(status < 0) return status; spidev_class = class_create(THIS_MODULE, "spidev");//创建设备文件 if(IS_ERR(spidev_class)) { unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name); return PTR_ERR(spidev_class); } status = spi_register_driver(&spidev_spi); if(status < 0) { class_destroy(spidev_class); unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name); } return status; } N_SPI_MINORS为手动定义的设备号,若大于256,则编译错误。