I am learning C++ through a Bjarne Stroustrup's "Programming Principles and Practice Using C++" and the first part of the drill of the chapter is to show the use of .h files. The drill is extremely simple and I followed it to the letter but I keep getting 'undefined reference to foo' when I try to compile. I will write the three files down bellow, they are super short.
by the way, std_lib_facilities.h is a .h file the author included that has a few includes in it like iostream as well as 'using namespace std;' and some functions the authors wrote that you need to use from time to time
my.h - contains extern int foo; void print_foo(); void print(int);
#ifndef MY_H
#define MY_H
extern int foo;
void print_foo();
void print(int);
#endif
my.cpp - includes my.h, defines print_foo() to print value of foo using cout and print(int i) to print the value of i using cout
#include "my.h"
#include "std_lib_facilities.h"
void print_foo()
{
cout << foo << '\n';
}
void print(int i)
{
cout << i << '\n';
}
use.cpp - includes my.h defines main() to set the value of foo to 7 and print it using print_foo() and print the value of 99 using print()
#include "my.h"
int main()
{
foo = 7;
print_foo();
print(99);
}
The OS I am using for this is Linux if that helps