Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

ImageCraft Products FAQ-O-Matic : All Compilers — V6 — may also apply to V7 : How do I handle global variables in a multi-file project?

0
10 Posted

ImageCraft Products FAQ-O-Matic : All Compilers — V6 — may also apply to V7 : How do I handle global variables in a multi-file project?

0
10

It is fairly simple – general rule is one file DEFINES the variable, and any number of files can reference the variables by DECLARING it. For example, in file1, you can write int foo; // this defines foo in file2, etc. you write extern int foo; // this declares foo A typical “trick” is to put something like this in a header file that all files: #include foo.h #ifdef MAIN #define EXTERN #else #define EXTERN extern #endif EXTERN int foo; and in file1.c, you write #define MAIN #include “foo.h” // this will define foo In file2.c and other files, you simply write #include “foo.h” // this will declare foo kristil@imagecraft.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123