1

I am trying to define global variable as shown in the printscreen and code bellow,but KEIL says

there is a problem with it ,something about static,what is the meaning? Where did i go wrong defining global variable? Thanks.

    ^
main.c(6): note: declare 'static' if the function is not intended to be used outside of this translation unit
void sysclockconfig(void)
^
static 
main.c(3): warning: no previous extern declaration for non-static variable 'counter' [-Wmissing-variable-declarations]
uint16_t counter;
         ^
main.c(3): note: declare 'static' if the variable is not intended to be used outside of this translation unit
uint16_t counter;
^
main.c(4): warning: no previous extern declaration for non-static variable 'k' [-Wmissing-variable-declarations]
uint16_t k;
         ^
main.c(4): note: declare 'static' if the variable is not intended to be used outside of this translation unit
uint16_t k;
^

enter image description here

#include "stm32f407xx.h"

uint16_t counter;
uint16_t k;

void sysclockconfig(void)
{
    //ENABLE HSE and wit for it till it becomes ready
 RCC->CR|=RCC_CR_HSEON;
 while(!(RCC->CR&RCC_CR_HSERDY));
    //Power enable
    RCC->APB1ENR|=RCC_APB1ENR_PWREN;
    //voltage regulator setting
    PWR->CR|=PWR_CR_VOS;
    //8:14
    //Flash control register
    FLASH->ACR=FLASH_ACR_ICEN|FLASH_ACR_DCEN|FLASH_ACR_PRFTEN|FLASH_ACR_LATENCY_5WS;
}

int main(void)
{
    
    
    while(1)
    {
        
    }
}
Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
ron398
  • 121
  • 1
  • 2

1 Answers1

2

I am guessing you are using the clang compiler with the -Wmissing-variable-declaration option, or some other option that includes it such as the (not recommended) -Weverything. The purpose of this is specifically to give a warning when a global variable definition is seen (uint16_t counter;) without previously having seen a declaration of it (extern uint16_t counter;).
For more information, see What is the difference between a definition and a declaration?

The idea is that in large programs, you would normally have the declaration in a header file (say foo.h), while the definition was in one of the .c source files (bar.c). You would then want to make sure you included foo.h into bar.c, so the compiler could make sure the definition and declaration matched (e.g. you didn't have extern short int x; in the header and long int x; in the source file). This option ensures that if you forget to include the header, you will get a warning.

This option is not very useful for small programs, so you'll probably want to disable it by changing the compilation options in your command line, Makefile or IDE. But you can also work around it by, as it suggests, declaring your counter variable as static uint16_t counter; which would prevent it from being used from any other source file (you don't have any others anyway). Or, if you want to keep it global, by adding extern uint16_t counter; on a previous line or in a header file. The same goes for k, of course.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82