I'm testing the how to use extern in C ,so I create three files for main.c, test.c, headfile.h . I want to declare variable and function in headfile.h,define in the test.c ,then print out the variable and call function at the main.c It works successfully by using Dev c++,however, when I put the exact same files into VScode it show errors that there are undefined reference to variables
the error messages enter image description here
main.c
#include <stdio.h>
#include <stdlib.h>
#include"D:\My Documents\Desktop\CODE\c\VScode\externTest\headfile.h"
int gVar = 1;
int main(void)
{
extern float a;
printf("a = %f\n",a);
printf("gVar = %d\n",gVar);
printf("aa = %d\n",aa);
printf("bb = %f\n",bb);
function ();
system("pause");
return 0;
}
test.c
#include <stdio.h>
#include "D:\My Documents\Desktop\CODE\c\VScode\externTest\headfile.h"
float a = 100;
int aa = 200;
float bb = 300;
void function (void){
printf("yeh you got it!!\n");
extern int gVar;
gVar++;
printf("gVar in test.c function = %d",gVar);
}
headfile.h
extern int aa;
extern float bb;
void function(void);