Help with header files

I'm just beginning to experiment with header files an I can't figure out why my compiler is giving me these 2 errors.

/*Error LNK1120 1 unresolved externals*/
/*Error LNK2001 unresolved external symbol "int foo" (?foo@@3HA)*/



use.cpp
1
2
3
4
5
6
7
8
#include "my.h";

int main()
{
	foo = 7;
	print_foo();
	print(99);
}


my.h
1
2
3
4
5
6
#include "std_lib_facilities.h";

extern int foo;
void print_foo();
void print(int);


my.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "std_lib_facilities.h";
#include "my.h";

void print_foo()
{
	cout << foo << endl;
}
void print(int i)
{
	cout << i << endl;
}
Last edited on
Your header has declared foo to exist somewhere, but it does not, in fact.

The purpose of the header is to tell the world what is available in the cpp module, so you should make foo available there.

my.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "std_lib_facilities.h";
#include "my.h";

int foo;

void print_foo()
{
	cout << foo << endl;
}
void print(int i)
{
	cout << i << endl;
}

Hope this helps.
Thank you very much! I appreciate the complete and concise explanation.
Topic archived. No new replies allowed.