首页 > 解决方案 > 函数类型更改时出现“错误:(1098)冲突声明”

问题描述

当函数类型从 int 更改为 void 时,我收到冲突声明错误。下面的代码给了我错误“TimerSetup.c:4: error: (1098) conflicting declarations for variable "_Timer1Init"”。但是,如果 Timer1Init 的函数类型在 TimerSetup.h 和 TimerSetup.c 文件中更改为 int,则错误消失。任何的想法!为什么?

定时器设置.h

// TimerSetup.h

    #ifndef XC_HEADER_TEMPLATE_H
        #define XC_HEADER_TEMPLATE_H

        #include <xc.h> // include processor files - each processor file is guarded.  
        #include <stdint.h>

        void Timer1Init();           //<-----Error vanish when type changed to int


        #ifdef  __cplusplus
        extern "C" {
        #endif /* __cplusplus */

            // TODO If C++ is being used, regular C code needs function names to have C 
            // linkage so the functions can be used by the c code. 

        #ifdef  __cplusplus
        }
        #endif /* __cplusplus */

        #endif  /* XC_HEADER_TEMPLATE_H */

TimerSetup.c

#include <xc.h>
#include "TimerSetup.h"

void Timer1Init()             //<-----Error vanish when type changed to int
{ 
    PIE1bits.TMR1IE=1;        
    PIR1bits.TMR1IF=0;   
} 

Wifi.c

   #include <xc.h>
    #include "Wifi.h"
    void eUart1Init()
    {   
        TRISB=0;
        TRISC6=0;        /* Make Tx pin as output*/
        TRISC7=1;        /* Make Rx pin as input*/
        TXSTA1 = 0;
        TXSTA1bits.BRGH    = 1;                // high baud rate
        BAUDCON1bits.BRG16 = 0;
        BAUDCON1 = 0;
        PMD0 = 0;

        TXSTA1bits.TX9 = 0;
        TXSTA1bits.TXEN = 1;  
        TXSTA1bits.SENDB = 0;
        TXSTA1bits.BRGH = 1;

        RCSTA1bits.SPEN = 1;
        RCSTA1bits.RX9 = 0;
        RCSTA1bits.CREN = 1;
        RCSTA2bits.CREN = 1;
        SPBRG1 = 51;

        IPR1bits.TX1IP = 0;    // Interrupt priority bit
        PIR1bits.TX1IF = 0;
        PIE1bits.TX1IE = 1; 

        IPR1bits.RC1IP = 0;    // Interrupt priority bit /////////
        PIR1bits.RC1IF = 0;
        PIE1bits.RC1IE = 1; 
    }
    void eUart2Init()
    {   
        TRISB6=0;        /* Make Tx2 pin as output*/
        TRISB7=1;        /* Make Rx2 pin as input*/
        TXSTA2 = 0;
        TXSTA2bits.BRGH    = 1;                // high baud rate
        BAUDCON2bits.BRG16 = 0;
        BAUDCON2 = 0;
        //PMD0 = 0;

        TXSTA2bits.TX9 = 0;
        TXSTA2bits.TXEN = 1;  
        TXSTA2bits.SENDB = 0;
        TXSTA2bits.BRGH = 1;

        RCSTA2bits.SPEN = 1;
        RCSTA2bits.RX9 = 0;
        RCSTA2bits.CREN = 1;
        SPBRG2 = 51;

        IPR3bits.TX2IP = 0;    // Interrupt priority bit
        PIR3bits.TX2IF = 0;
        PIE3bits.TX2IE = 1; 

        IPR3bits.RC2IP = 0;    // Interrupt priority bit
        PIR3bits.RC2IF = 0;
        PIE3bits.RC2IE = 1; 
    }
    // EUART1 Put String Function Defines Here
    void eUart1Puts(const char *s)
    {
        while(*s)
            eUart1PutByte(*s++);
    }
    void eUart2Puts(const char *s)
    {
        while(*s)
            eUart2PutByte(*s++);
    }
    void eUart1PutByte(unsigned char byte)
    {
    while((PIR1bits.TX1IF != 1) && (TXSTA1bits.TXEN1 != 0)) // set when register is empty
            continue;
        PIE1bits.TX1IE = 1;
        TXREG1 = byte;
    }
    void eUart2PutByte(unsigned char byte)
    {
    while((PIR3bits.TX2IF != 1) && (TXSTA2bits.TXEN2 != 0)) // set when register is empty
            continue;
        PIE1bits.TX1IE = 1;
        TXREG2 = byte;
    }
    void WifiAT(char *command)
    {
        eUart1Puts(command);
        //__delay_ms(wifi_wait);
        while (eusart_rx_buffer_rd != eusart_rx_buffer_wr) 
            {   // While there's data in the buffer
                eUart2PutByte(eusart_rx_buffer[eusart_rx_buffer_rd++]);   // Do something with it

                if (eusart_rx_buffer_rd >= EUSART_BUFFER_SIZE)     // Increase read pointer
                {
                    eusart_rx_buffer_rd = 0;
                }
            }            
    }
    __interrupt() void ISR(void)             //<----------------------- To here
    {
        if((PIR3bits.TX2IF == 1) && (PIE3bits.TX2IE == 1))  //Debug Terminal Tx  
        {
            PIR3bits.TX2IF = 0;
            PIE3bits.TX2IE = 0;
        }

        if((PIR3bits.RC2IF != 0) && (PIE3bits.RC2IE != 0))  //Debug Terminal Rx
        {         
            PIR3bits.RC2IF = 0;
            rc2_data = RCREG2;      
        }

        if((PIR1bits.TX1IF != 0) && (PIE1bits.TX1IE != 0))        
        {
            PIR1bits.TX1IF = 0;
            PIE1bits.TX1IE = 0;
        }

        if((PIR1bits.RC1IF != 0) && (PIE1bits.RC1IE != 0))
        {         
    //      rc_data = RCREG1;
    //      eUart2PutByte(rc_data);

            eusart_rx_buffer[eusart_rx_buffer_wr++] = RCREG1;  // Store the received data

            if (eusart_rx_buffer_wr >= EUSART_BUFFER_SIZE)    // Increment write pointer
            {
                eusart_rx_buffer_wr = 0;
            }

            PIR1bits.RC1IF = 0;
        }

    //    if((PIR1bits.TMR1IF != 0) && (PIE1bits.TMR1IE != 0))
    //    {
    //        int flag = 0;
    //        TMR1=0xF856;
    //        if(flag >= 1000)
    //        {
    //            PORTCbits.RC1 = ~(PORTCbits.RC1);
    //            PIR1bits.TMR1IF=0;  /* Make Timer1 Overflow Flag to '0' */   
    //        }
    //    }
    }

TokMain.c

    #include <xc.h>
#include "Wifi.h"

void eUart1Init()
{   
    TRISB=0;
    TRISC6=0;        /* Make Tx pin as output*/
    TRISC7=1;        /* Make Rx pin as input*/
    TXSTA1 = 0;
    TXSTA1bits.BRGH    = 1;                // high baud rate
    BAUDCON1bits.BRG16 = 0;
    BAUDCON1 = 0;
    PMD0 = 0;

    TXSTA1bits.TX9 = 0;
    TXSTA1bits.TXEN = 1;  
    TXSTA1bits.SENDB = 0;
    TXSTA1bits.BRGH = 1;

    RCSTA1bits.SPEN = 1;
    RCSTA1bits.RX9 = 0;
    RCSTA1bits.CREN = 1;
    RCSTA2bits.CREN = 1;

    SPBRG1 = 51;

    IPR1bits.TX1IP = 0;    // Interrupt priority bit
    PIR1bits.TX1IF = 0;
    PIE1bits.TX1IE = 1; 

    IPR1bits.RC1IP = 0;    // Interrupt priority bit /////////
    PIR1bits.RC1IF = 0;
    PIE1bits.RC1IE = 1; 
}

void eUart2Init()
{   
    TRISB6=0;        /* Make Tx2 pin as output*/
    TRISB7=1;        /* Make Rx2 pin as input*/
    TXSTA2 = 0;
    TXSTA2bits.BRGH    = 1;                // high baud rate
    BAUDCON2bits.BRG16 = 0;
    BAUDCON2 = 0;
    //PMD0 = 0;

    TXSTA2bits.TX9 = 0;
    TXSTA2bits.TXEN = 1;  
    TXSTA2bits.SENDB = 0;
    TXSTA2bits.BRGH = 1;

    RCSTA2bits.SPEN = 1;
    RCSTA2bits.RX9 = 0;
    RCSTA2bits.CREN = 1;

    SPBRG2 = 51;

    IPR3bits.TX2IP = 0;    // Interrupt priority bit
    PIR3bits.TX2IF = 0;
    PIE3bits.TX2IE = 1; 

    IPR3bits.RC2IP = 0;    // Interrupt priority bit
    PIR3bits.RC2IF = 0;
    PIE3bits.RC2IE = 1; 
}
// EUART1 Put String Function Defines Here
void eUart1Puts(const char *s)
{
    while(*s)
        eUart1PutByte(*s++);
}

void eUart2Puts(const char *s)
{
    while(*s)
        eUart2PutByte(*s++);
}

void eUart1PutByte(unsigned char byte)
{
while((PIR1bits.TX1IF != 1) && (TXSTA1bits.TXEN1 != 0)) // set when register is empty
        continue;
    PIE1bits.TX1IE = 1;
    TXREG1 = byte;
}

void eUart2PutByte(unsigned char byte)
{
while((PIR3bits.TX2IF != 1) && (TXSTA2bits.TXEN2 != 0)) // set when register is empty
        continue;
    PIE1bits.TX1IE = 1;
    TXREG2 = byte;
}

void WifiAT(char *command)
{
    eUart1Puts(command);
    //__delay_ms(wifi_wait);
    while (eusart_rx_buffer_rd != eusart_rx_buffer_wr) 
        {   // While there's data in the buffer
            eUart2PutByte(eusart_rx_buffer[eusart_rx_buffer_rd++]);   // Do something with it

            if (eusart_rx_buffer_rd >= EUSART_BUFFER_SIZE)     // Increase read pointer
            {
                eusart_rx_buffer_rd = 0;
            }
        }            
}

__interrupt() void ISR(void)             //<----------------------- To here
{
    if((PIR3bits.TX2IF == 1) && (PIE3bits.TX2IE == 1))  //Debug Terminal Tx  
    {
        PIR3bits.TX2IF = 0;
        PIE3bits.TX2IE = 0;
    }

    if((PIR3bits.RC2IF != 0) && (PIE3bits.RC2IE != 0))  //Debug Terminal Rx
    {         
        PIR3bits.RC2IF = 0;
        rc2_data = RCREG2;      
    }

    if((PIR1bits.TX1IF != 0) && (PIE1bits.TX1IE != 0))        
    {
        PIR1bits.TX1IF = 0;
        PIE1bits.TX1IE = 0;
    }

    if((PIR1bits.RC1IF != 0) && (PIE1bits.RC1IE != 0))
    {         
//      rc_data = RCREG1;
//      eUart2PutByte(rc_data);

        eusart_rx_buffer[eusart_rx_buffer_wr++] = RCREG1;  // Store the received data

        if (eusart_rx_buffer_wr >= EUSART_BUFFER_SIZE)    // Increment write pointer
        {
            eusart_rx_buffer_wr = 0;
        }

        PIR1bits.RC1IF = 0;
    }

//    if((PIR1bits.TMR1IF != 0) && (PIE1bits.TMR1IE != 0))
//    {
//        int flag = 0;
//        TMR1=0xF856;
//        if(flag >= 1000)
//        {
//            PORTCbits.RC1 = ~(PORTCbits.RC1);
//            PIR1bits.TMR1IF=0;  /* Make Timer1 Overflow Flag to '0' */   
//        }
//    }
}

Wifi.h

// This is a guard condition so that contents of this file are not included
// more than once.  
#ifndef XC_HEADER_TEMPLATE_H
#define XC_HEADER_TEMPLATE_H

#include <xc.h> // include processor files - each processor file is guarded.  
#include <stdint.h>

#define _XTAL_FREQ 8000000UL
#define EUSART_BUFFER_SIZE 1024

char rc2_data; 
char rc_data;

char eusart_rx_buffer[EUSART_BUFFER_SIZE];   // the actual buffer, now 2048 bytes long
uint16_t eusart_rx_buffer_rd = 0;            // the current read position
uint16_t eusart_rx_buffer_wr = 0;            // the current write position

void eUart1Init();
void eUart2Init();
void eUart1PutByte(unsigned char byte);
void eUart2PutByte(unsigned char byte);
void eUart1Puts(const char *s);
void eUart2Puts(const char *s);
void WifiAT(char *command);


// TODO Insert declarations or function prototypes (right here) to leverage 
// live documentation

#ifdef  __cplusplus
extern "C" {
#endif /* __cplusplus */

    // TODO If C++ is being used, regular C code needs function names to have C 
    // linkage so the functions can be used by the c code. 

#ifdef  __cplusplus
}
#endif /* __cplusplus */

#endif  /* XC_HEADER_TEMPLATE_H */

完整的错误消息:

make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'E:/pk/TokenOverWiFi.X'
make  -f nbproject/Makefile-default.mk dist/default/production/TokenOverWiFi.X.production.hex
make[2]: Entering directory 'E:/pk/TokenOverWiFi.X'
"C:\Program Files (x86)\Microchip\xc8\v1.44\bin\xc8.exe" --pass1  --chip=18F25K80 -Q -G  --double=24 --float=24 --emi=wordwrite --opt=+asm,+asmfile,-speed,+space,-debug,-local --addrqual=ignore --mode=free -P -N255 --warn=-3 --asmlist -DXPRJ_default=default  --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,-plib   --output=-mcof,+elf:multilocs --stack=compiled:auto:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s"     -obuild/default/production/TokMain.p1 TokMain.c 
TokMain.c:81: warning: (361) function declared implicit int
"C:\Program Files (x86)\Microchip\xc8\v1.44\bin\xc8.exe"  --chip=18F25K80 -G -mdist/default/production/TokenOverWiFi.X.production.map  -DXPRJ_default=default  --double=24 --float=24 --emi=wordwrite --opt=+asm,+asmfile,-speed,+space,-debug,-local --addrqual=ignore --mode=free -P -N255 --warn=-3 --asmlist --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,-plib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s"      --memorysummary dist/default/production/memoryfile.xml -odist/default/production/TokenOverWiFi.X.production.elf  build/default/production/TokMain.p1 build/default/production/Wifi.p1 build/default/production/TimerSetup.p1     
Microchip MPLAB XC8 C Compiler (Free Mode) V1.44
Build date: Sep 13 2017
Part Support Version: 1.44
Copyright (C) 2017 Microchip Technology Inc.
License type: Node Configuration

:: warning: (1273) Omniscient Code Generation not available in Free mode
TimerSetup.c:4: error: (1098) conflicting declarations for variable "_Timer1Init" (TokMain.c:81)
make[2]: *** [dist/default/production/TokenOverWiFi.X.production.hex] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
(908) exit status = 1
nbproject/Makefile-default.mk:163: recipe for target 'dist/default/production/TokenOverWiFi.X.production.hex' failed
make[2]: Leaving directory 'E:/pk/TokenOverWiFi.X'
nbproject/Makefile-default.mk:90: recipe for target '.build-conf' failed
make[1]: Leaving directory 'E:/pk/TokenOverWiFi.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed

BUILD FAILED (exit value 2, total time: 805ms)

编辑

当我注释掉我的其他头文件时,代码编译成功。如何??

标签: cembeddedmplab

解决方案


问题是您在每个头文件中对头保护使用相同的名称,因此您阻止编译器查看大部分代码。

#ifndef XC_HEADER_TEMPLATE_H
#define XC_HEADER_TEMPLATE_H

这应该是类似的东西#ifndef WIFI_H#ifndef TIMER_SETUP_H等等。为每个头文件选择一个唯一的名称。


推荐阅读