Page 141 - C-Language
P. 141
int main(void)
{
foo(42, "bar");
return 0;
}
Compile and Link
First, we compile both foo.c and main.c to object files. Here we use the gcc compiler, your compiler
may have a different name and need other options.
$ gcc -Wall -c foo.c
$ gcc -Wall -c main.c
Now we link them together to produce our final executable:
$ gcc -o testprogram foo.o main.o
Using a Global Variable
Use of global variables is generally discouraged. It makes your program more difficult to
understand, and harder to debug. But sometimes using a global variable is acceptable.
global.h
#ifndef GLOBAL_DOT_H /* This is an "include guard" */
#define GLOBAL_DOT_H
/**
* This tells the compiler that g_myglobal exists somewhere.
* Without "extern", this would create a new variable named
* g_myglobal in _every file_ that included it. Don't miss this!
*/
extern int g_myglobal; /* _Declare_ g_myglobal, that is promise it will be _defined_ by
* some module. */
#endif /* GLOBAL_DOT_H */
global.c
#include "global.h" /* Always include the header file that declares something
* in the C file that defines it. This makes sure that the
* declaration and definition are always in-sync.
*/
int g_myglobal; /* _Define_ my_global. As living in global scope it gets initialised to 0
* on program start-up. */
main.c
#include "global.h"
https://riptutorial.com/ 117

