How to use global variables and their confirmation
*** I received a lot of advice from @fujitanozomu, so I revised this memo significantly. I want to keep the revision history as much as possible, but I could not display the difference of the program file, so I changed only the article. *** ***
main.c: ~~ global ~~ ~~ declaration of variable num ~~ A and B *** in definition and order (both A and B are external functions. Check if the variable is definitely global externally) ** * Process *** (Addition) main.h: Declare the variable num with extern. For other c files, the variable num becomes global by including this header. *** *** A.h: ~~ Declares to use global variable num ~~ Declares function A to be used externally A.c: Include *** main.h and change *** num to output (global variable num is changed) B.h: ~~ Declares to use global variable num ~~ Declares function B to be used externally B.c: Include *** main.h and output *** without changing num (output global variable num changed in A)
In the above file, num declared in main can be used from A and B (because it becomes a global variable), and even if num is changed in A, it is proved that the change can be confirmed from B as well.
main.c
#include"main.h"
#include"A.h" //To use the function of A
#include"B.h" //To use B's function
int num; //Declare a variable
int main(int argc, char *argv[]){
A(); //Have num changed in another file
B(); //Check for changed num in other files
return 0;
}
main.h
extern int num; //Other files (here is main.It means that num is defined in c)
A.h
extern void A(); //A.Function A defined in c is declared in extern for external use
A.c
#include<stdio.h>
#include"A.h"
#include"main.h" //Where num becomes global
void A(){
num = 10; //Change global variables
printf("inside A, num = %d\n", num); //Change global variable and output
}
B.h
extern void B(); //B.Function B defined in c is declared in extern for external use
B.c
#include<stdio.h>
#include"B.h"
#include"main.h" //Where num becomes global
void B(){
printf("inside B, num = %d\n", num); //Output without changing global variables
}
#Create shared library for A
[pirlo@centos8 02]$ gcc -shared -o libA.so -c A.c
#Create shared library for B
[pirlo@centos8 02]$ gcc -shared -o libB.so -c B.c
#Compile the main process and create an executable file
[pirlo@centos8 02]$ gcc -o main main.c -L. -lA -lB
#File list
[pirlo@centos8 02]$ ls
A.c A.h B.c B.h libA.so libB.so main main.c main.h
Execution result
[pirlo@centos8 02]$ ./main
inside A, num = 10 #Change global variable with A
inside B, num = 10 #Check the global variables changed from B to A
*** Addendum ***
https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
*** According to this article, functions are different from variables, and when compiled, the compiler treats them as if they had externs, so it seems that you don't have to specify externs. When I actually tested it, I was able to compile and execute it even if I deleted #include "A.h" and #include "B.h" from main.h. *** ***
Recommended Posts