C Use variable defined in file being included from

I am currently googling this, but I don't know how to word it. I suspect someone will point me to a duplicate question. But... if I have a file like so

parent.h:

...
include "child.h";
int foo;
...

and the following source file, which is called above via its header file:

child.c:

int display ()
{
    printf ( "%d\n", foo );
}

Now this is a simplified example, currently I am using C++ and I am creating an object, and that object calls a method of another object, like foo declared in the parent file. Naturally I get the following error:

error: ‘foo’ was not declared in this scope

Is there any way to get around this scope issue, or must I pass foo down as a parameter?

Solutions

I believe you are looking for extern.

EDIT:

Adding some initial linkage.

Usually, variables live in the source files...

parent.c

int foo = 0;

...and are externed in the header files...

parent.h

extern int foo;

In this way, whomever #includes parent.h will extern foo.

Tags: C++ / C / Include

Similar questions

C Header file being included in another header file and both included in a c file
I have a question regarding header files in C. Let's say I have a header file containing a structure car called cars.h, as follows: Now, I have another header file which includes the above header, called drivers.h, as follows: Now, let's say I have another file, but this time it is a C file, called main.c, as follow: Could you explain why this is w...
math.h not being included even when it is included in makefile
I am trying to build SPRO from http://www.irisa.fr/metiss/guig/spro They use Automake 1.6.2 to generate the makefile. My issue is that when I am trying to make the project I am getting the error "undefined reference to 'sin' " etc. Here is the error image This error is due to the fact that math library is not being included with -lm flag but -lm is...
Should a header be included in a file if that header is already implicitly included?
I am wondering: should a header be included in a file if that header is already implicitly included? For example dataStructure.h utils.h main.c If later GetSize method is deleted in utils.h file, utils.h no longer needs to include dataStructure.h. Then after these modifications suddenly main.c can not compile without the dataStructure.h header. A r...
Is it common practive to #include header files already included by included header files?
Let's say we have a header file A.h which depends on things declared in B.h and C.h. B.h also depends an C.h and therefore includes it. In this case we don't need to include C.h in A.h and it will compile just fine without it. But I am wondering what the best course of action is in these cases. If B.h somehow changes and no longer depends on C.h, A...
The variable 'buff' is being used without being defined
I need to read some data from text file and store it in 2D-array. This code works good: But why doesn't it work when I try to change buff definition in line 5 to: I expected this definition to work too. The error I get:
Is there any possible use of using extern variable in a file that is not included in any other file?
I've encountered many examples on the web, that have extern int x in main.c, where the main function lies. The only use of extern I'm aware of, is to make a declaration in another file, and use it in another file, after defining it. Like : a.h : extern int x; a.c : int x = 5; main.c : #include "a.h" // and start using x The 1st case seems redundant...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.