关于C30编译器外部变量使用问题

2020-02-08 12:28发布

小弟最近在学习PIC单片机。现在玩的开发板上的单片机型号为PIC24F16KA102。编了个简单的程序,目的是使用外部变量,可怎么弄编译都不通过。
现象如下:

下面是main.c。(不必细看,此文件 #include "U2.h" ,主要是想使用U2.h里定义的变量tt)

#include <p24F16KA102.h>
#include "U2.h"

//函数功能:I/O端口初始化
//入口参数:无
//出口参数:无
void IOInit()
{
        AD1PCFG = 0xFFFF;                //关闭PortB端口所有引脚的模拟功能
        _TRISB8 = 0;                        //RB8输出
        _TRISB15 = 0;                        //RB15输出
}

int main (void)
{
        IOInit();

        while(1)
        {
                _LATB8 = 0;
                _LATB15 = 1;
                DelaymS(DELAY_NUM);
                _LATB8 = 1;
                _LATB15 = 0;
                DelaymS(DELAY_NUM);
                tt++;
        }
}



下面是U2.c文件。(不必细看,此文件也包含了U2.h)

#include "U2.h"

//函数功能:延时程序(毫秒)
//入口参数:del(数值范围:16位整型)
//出口参数:无
void DelaymS(unsigned int del)
{
        unsigned int j;
        while(del--)
      for(j=0;j<1600;j++);
}




下面是U2.h文件

#ifndef __U2_H
#define __U2_H

#define DELAY_NUM  100

unsigned char tt;        //想在此定义一个变量tt,在main.c和H2.c中都可以引用到
                         //此处没有声明extern ,有声明extern 的错误情况在下面有说明。

void DelaymS(unsigned int del);

#endif


下面是编译后的结果。
----------------------------------------------------------------------
Debug build of project `F:CZH est est2 est1.mcp' started.
Language tool versions: pic30-as.exe v3.22, pic30-gcc.exe v3.22, pic30-ld.exe v3.22, pic30-ar.exe v3.22
Preprocessor symbol `__DEBUG' is defined.
Target debug platform is `__MPLAB_DEBUGGER_PK3=1'.
Debug platform options are: `__ICD2RAM=1'.
Sat Jun 11 08:56:08 2011
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Deleted file "F:CZH est est2main.o".
Clean: Deleted file "F:CZH est est2U2.o".
Clean: Deleted file "F:CZH est est2 est1.cof".
Clean: Deleted file "F:CZH est est2 est1.hex".
Clean: Done.
Executing: "d:Program FilesMicrochipMPLAB C30inpic30-gcc.exe" -mcpu=24F16KA102 -x c -c   "main.c" -o"main.o" -D__DEBUG -D__MPLAB_DEBUGGER_PK3=1 -g -Wall -O1
Microchip MPLAB C30 License Manager Version v3_22 (Build Date Dec  8 2009).
Copyright (c) 2008 Microchip Technology Inc. All rights reserved.
The MPLAB C30 license has expired.
Options have been disabled due to expired license
Visit http://www.microchip.com/ to purchase a new key.Executing: "d:Program FilesMicrochipMPLAB C30inpic30-gcc.exe" -mcpu=24F16KA102 -x c -c   "U2.c" -o"U2.o" -D__DEBUG -D__MPLAB_DEBUGGER_PK3=1 -g -Wall -O1
Microchip MPLAB C30 License Manager Version v3_22 (Build Date Dec  8 2009).
Copyright (c) 2008 Microchip Technology Inc. All rights reserved.
The MPLAB C30 license has expired.
Options have been disabled due to expired license
Visit http://www.microchip.com/ to purchase a new key.Executing: "d:Program FilesMicrochipMPLAB C30inpic30-gcc.exe" -mcpu=24F16KA102 "main.o" "U2.o" -o"test1.cof" -Wl,--script="D:Program FilesMicrochipMPLAB C30supportPIC24Fgldp24F16KA102.gld",--defsym=__MPLAB_BUILD=1,--defsym=__MPLAB_DEBUG=1,--defsym=__MPLAB_DEBUGGER_PK3=1,--defsym=__ICD2RAM=1,--heap=0,-Map="test1.map",--report-mem
Microchip MPLAB C30 License Manager Version v3_22 (Build Date Dec  8 2009).
Copyright (c) 2008 Microchip Technology Inc. All rights reserved.
The MPLAB C30 license has expired.
Options have been disabled due to expired license
Visit http://www.microchip.com/ to purchase a new key.U2.o(.nbss+0x0):F:CZH est est2U2.c: multiple definition of `tt'
main.o(.nbss+0x0):F:CZH est est2main.c: first defined here
d:program filesmicrochipmplab c30inin..in/pic30-coff-ld.exe: Link terminated due to previous error(s).
Link step failed.
----------------------------------------------------------------------
Debug build of project `F:CZH est est2 est1.mcp' failed.
Language tool versions: pic30-as.exe v3.22, pic30-gcc.exe v3.22, pic30-ld.exe v3.22, pic30-ar.exe v3.22
Preprocessor symbol `__DEBUG' is defined.
Target debug platform is `__MPLAB_DEBUGGER_PK3=1'.
Debug platform options are: `__ICD2RAM=1'.
Sat Jun 11 08:56:10 2011
----------------------------------------------------------------------
BUILD FAILED

我看了一下错误原因,是说变量tt重复定义了。可是我在U2.h中已经对重复定义进行了条件编译
#ifndef __U2_H
#define __U2_H
...
#endif


然后我把U2.h中的tt定义加了个extern声明。即为:extern unsigned char tt;

编译后的结果如下:
----------------------------------------------------------------------
Debug build of project `F:CZH est est2 est1.mcp' started.
Language tool versions: pic30-as.exe v3.22, pic30-gcc.exe v3.22, pic30-ld.exe v3.22, pic30-ar.exe v3.22
Preprocessor symbol `__DEBUG' is defined.
Target debug platform is `__MPLAB_DEBUGGER_PK3=1'.
Debug platform options are: `__ICD2RAM=1'.
Sat Jun 11 09:33:49 2011
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Deleted file "F:CZH est est2main.o".
Clean: Deleted file "F:CZH est est2U2.o".
Clean: Done.
Executing: "d:Program FilesMicrochipMPLAB C30inpic30-gcc.exe" -mcpu=24F16KA102 -x c -c   "main.c" -o"main.o" -D__DEBUG -D__MPLAB_DEBUGGER_PK3=1 -g -Wall -O1
Microchip MPLAB C30 License Manager Version v3_22 (Build Date Dec  8 2009).
Copyright (c) 2008 Microchip Technology Inc. All rights reserved.
The MPLAB C30 license has expired.
Options have been disabled due to expired license
Visit http://www.microchip.com/ to purchase a new key.Executing: "d:Program FilesMicrochipMPLAB C30inpic30-gcc.exe" -mcpu=24F16KA102 -x c -c   "U2.c" -o"U2.o" -D__DEBUG -D__MPLAB_DEBUGGER_PK3=1 -g -Wall -O1
Microchip MPLAB C30 License Manager Version v3_22 (Build Date Dec  8 2009).
Copyright (c) 2008 Microchip Technology Inc. All rights reserved.
The MPLAB C30 license has expired.
Options have been disabled due to expired license
Visit http://www.microchip.com/ to purchase a new key.Executing: "d:Program FilesMicrochipMPLAB C30inpic30-gcc.exe" -mcpu=24F16KA102 "main.o" "U2.o" -o"test1.cof" -Wl,--script="D:Program FilesMicrochipMPLAB C30supportPIC24Fgldp24F16KA102.gld",--defsym=__MPLAB_BUILD=1,--defsym=__MPLAB_DEBUG=1,--defsym=__MPLAB_DEBUGGER_PK3=1,--defsym=__ICD2RAM=1,--heap=0,-Map="test1.map",--report-mem
Microchip MPLAB C30 License Manager Version v3_22 (Build Date Dec  8 2009).
Copyright (c) 2008 Microchip Technology Inc. All rights reserved.
The MPLAB C30 license has expired.
Options have been disabled due to expired license
Visit http://www.microchip.com/ to purchase a new key.

Program Memory  [Origin = 0x200, Length = 0x29ff]

section                    address   length (PC units)   length (bytes) (dec)
-------                    -------   -----------------   --------------------
.text                        0x200                0x90            0xd8  (216)
.text                        0x290                0x30            0x48  (72)
.dinit                       0x2c0                 0x2             0x3  (3)
.isr                         0x2c2                 0x2             0x3  (3)

                     Total program memory used (bytes):          0x126  (294) 1%


Data Memory  [Origin = 0x800, Length = 0x600]

section                    address      alignment gaps    total length  (dec)
-------                    -------      --------------    -------------------
.icd                         0x800                0x50            0x50  (80)

                        Total data memory used (bytes):           0x50  (80) 5%


Dynamic Memory Usage

region                     address                      maximum length  (dec)
------                     -------                      ---------------------
heap                             0                                   0  (0)
stack                        0x850                               0x5b0  (1456)

                        Maximum dynamic memory (bytes):          0x5b0  (1456)

main.o(.text+0x1a): In function `main':
F:CZH est est2main.c:38: undefined reference to `tt'
Link step failed.
----------------------------------------------------------------------
Debug build of project `F:CZH est est2 est1.mcp' failed.
Language tool versions: pic30-as.exe v3.22, pic30-gcc.exe v3.22, pic30-ld.exe v3.22, pic30-ar.exe v3.22
Preprocessor symbol `__DEBUG' is defined.
Target debug platform is `__MPLAB_DEBUGGER_PK3=1'.
Debug platform options are: `__ICD2RAM=1'.
Sat Jun 11 09:33:51 2011
----------------------------------------------------------------------
BUILD FAILED

报错是说main.c中没有声明tt。

是不是重复定义不是这种格式啊,请大侠指点。谢谢!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
4条回答
cao_zhihong_0
1楼-- · 2020-02-08 15:39
 精彩回答 2  元偷偷看……
sunshine2011
2楼-- · 2020-02-08 16:49
在main.c 或者 U2.c中添加 unsigned char tt;
guan
3楼-- · 2020-02-08 20:53
dddddddddddddddddddddddddddd
visharn
4楼-- · 2020-02-08 22:30
声明前面 加extern关键字

一周热门 更多>