error: a label can only be part of a statement and a declaration is not a statement

在开发代码中使用switch出现的一个编译错误:

error: a label can only be part of a statement and a declaration is not a statement

代码片段如下所示:

    switch(cmd)
    {
        case EDMA3_DRV_IOCTL_DMA_BUFFER_ALLOC:

            dma_buf_info_t *dma_buf_info = (dma_buf_info_t *)arg;
            
            if(NULL == dma_buf_info
                || NULL == dma_buf_info->buf_info)
            {
                RPT(RPT_ERR, "null pointer,err");
                return -1;
            }

            for(i=0; i<dma_buf_info->num_buffers; i++)
            {
                dma_buf_info->buf_info[i].virtAddr_kernel_space = (uint8_t *)dma_alloc_coherent(NULL, 
                                                         dma_buf_info->buffer_size, 
                                                         (dma_addr_t *)&dma_buf_info->buf_info[i].phyAddr, 
                                                         GFP_KERNEL);
                if(0 == dma_buf_info->buf_info[i].phyAddr)
                {
                    RPT(RPT_ERR, "alloc dma buffer,error!!  phyAddr == 0");
                    return -1;
                }

                RPT(RPT_DBG,
                "Allocated DMA phy memory: Phy Address: 0x%llx Virtual Address :0x%p Size :0x%x successfull!!",
                                                            dma_buf_info->buf_info[i].phyAddr,
                                                            dma_buf_info->buf_info[i].virtAddr_kernel_space, 
                                                           (unsigned int)dma_buf_info->buffer_size);
            }
 
            break;

修改方法,在case 后加大括号将代码括起来,如下所示

    switch(cmd)
    {
        case EDMA3_DRV_IOCTL_DMA_BUFFER_ALLOC:
        {
            dma_buf_info_t *dma_buf_info = (dma_buf_info_t *)arg;
            
            if(NULL == dma_buf_info
                || NULL == dma_buf_info->buf_info)
            {
                RPT(RPT_ERR, "null pointer,err");
                return -1;
            }

            for(i=0; i<dma_buf_info->num_buffers; i++)
            {
                dma_buf_info->buf_info[i].virtAddr_kernel_space = (uint8_t *)dma_alloc_coherent(NULL, 
                                                         dma_buf_info->buffer_size, 
                                                         (dma_addr_t *)&dma_buf_info->buf_info[i].phyAddr, 
                                                         GFP_KERNEL);
                if(0 == dma_buf_info->buf_info[i].phyAddr)
                {
                    RPT(RPT_ERR, "alloc dma buffer,error!!  phyAddr == 0");
                    return -1;
                }

                RPT(RPT_DBG,
                "Allocated DMA phy memory: Phy Address: 0x%llx Virtual Address :0x%p Size :0x%x successfull!!",
                                                            dma_buf_info->buf_info[i].phyAddr,
                                                            dma_buf_info->buf_info[i].virtAddr_kernel_space, 
                                                           (unsigned int)dma_buf_info->buffer_size);
            }
        }   
            break;

解决

你可能感兴趣的:(编译错误解决)