This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

6678全局变量的定义



我用某个.c文件

#pragma  DATA_SECTION(flag, ".my_sec");      //定义到SL2:   o = 0x0C000000  l = 0x00200000
#pragma  DATA_ALIGN(flag, CACHE_L1D_LINESIZE);
 volatile uint32_t  flag;    

定义全局变量后,在看另一个.c 用extern重新定义引用,但是发现在这个.c flag的地址不在SL2上。难道不能这么用吗?

之后我改了一下在头文件内定义

#pragma  DATA_SECTION(flag, ".my_sec");      //定义到SL2:   o = 0x0C000000  l = 0x00200000
#pragma  DATA_ALIGN(flag, CACHE_L1D_LINESIZE);
 static volatile uint32_t  flag;    

这样虽然俩个.c 都可以引用flag,而且俩个。c的flag的地址都在SL2上,但是地址不是一个。。

我想知道,怎样在俩个。c里用同一个全局变量,各位牛人之前遇到过这种情况吗?

  • 第二种肯定不行,static定义了这个变量范围在文件内,两个文件会产生两个实体;

    第一种应该是可以的,我怀疑是你的程序里有别的位置也定义了flag变量,你把这里的定义修改成其它名字试试,比如my_flag,再编译试试。

  • 还是不行啊。。你帮我看看问题处在哪?

    下面是警告:

     

    还有我是这么定义的:

    一个c文件:#pragma  DATA_SECTION(my_flag, ".my_sec");
    #pragma  DATA_ALIGN(my_flag, CACHE_L1D_LINESIZE);
     volatile uint32_t  my_flag;

    另一个C文件:extern volatile uint32_t my_flag;

    cmd文件是:

    MEMORY
    {
     /* Local L2, 0.5~1MB*/
     VECTORS:  o = 0x00800000  l = 0x00000200
     LL2_RW_DATA:  o = 0x00800200  l = 0x0007FE00
      // LL2_RW_DATA:  o = 0x00800200  l = 0x0007FE00
     /* Shared L2 2~4MB*/
     //SL2:   o = 0x0C000000  l = 0x00200000
        SL2:   o = 0x0C000000  l = 0x00200000
        my_sect:  o = 0x0C200000  l = 0x000FF000
     /* External DDR3, upto 2GB per core */
     DDR3_CODE:  o = 0x80000000  l = 0x01000000   /*set memory protection attribitue as execution only*/
     DDR3_R_DATA:  o = 0x81000000  l = 0x01000000   /*set memory protection attribitue as read only*/
     DDR3_RW_DATA:  o = 0x82000000  l = 0x06000000   /*set memory protection attribitue as read/write*/
     EMIF16_DATA:    o = 0x70000000  l = 0x10000000  /* EMIF16 memory space */
    }

    SECTIONS
    {
     vecs        >    VECTORS

     .text           >    SL2
     .cinit          >    LL2_RW_DATA
     .const          >    LL2_RW_DATA
     .switch          >    LL2_RW_DATA

     .stack          >    LL2_RW_DATA
     GROUP
     {
      .neardata
      .rodata
      .bss
     }           >    LL2_RW_DATA
     .far:testBuf   >    SL2
     .far           >     LL2_RW_DATA
     .fardata        >    SL2
     .cio           >     LL2_RW_DATA
     .sysmem        >    LL2_RW_DATA
     .my_sec          >   my_sect

    }

    然后运行程序:

      printf("my_flag=%d\n",my_flag);
      printf("my_flag=%d\n",&my_flag);

    结果在俩个c文件内打印出来的值不一样;一致性 我也维护了。。第一个c文件在 sl2上,第二个在LL2;这是什么原因?求Allen帮忙解答..