Here is how you would use extern
variables.
I have a UART.c:
#include<stdio.h>
#include<string.h>
#include<UART.h>
int UART_Rcv(unsigned char* Rx_Str[], unsigned int len);
int status;
int UART_Rcv(unsigned char* Rx_Str[], unsigned int len)
{
//Some code for implementation of UART receive.
//set status value and return it, May be (SUCCESS or FAILURE?)
return status;
}
Now I have a UART.h
//header guard
#ifndef __UART_H
#define __UART_H
//Some UART related macros
extern int status;
extern int UART_Rcv(unsigned char* Rx_Str[], unsigned int len);
//other externs if any
#endif
Now I have a main.c which is going to use the function and variable declared in UART.c
#include<stdio.h>
#include<string.h>
#include<UART.h>
#define WIMP_OUT_N_GO_HOME (0)
int main(void)
{
unsigned char Received_String[30];
//Some code and may be initialization of UART?
//Flush the Received_String before using it
UART_Rcv(Received_String, 10);
//Some code
return WIMP_OUT_N_GO_HOME;
}
When a variable is defined, the compiler allocates memory for that variable and possibly also initializes its contents to some value. When a variable is declared, the compiler requires that the variable be defined elsewhere. So By extern
ing your UART_Rcv()
function and the status
variable, you hinted your compiler that those are defined outside of the functional block, or may be in a different source, like we did here. the definitions for those will be found at the linker time.
An external variable must be defined, exactly once, outside of any function