I'm working through exercises from Programming Principles and Practice using Visual Studio 2012. When trying to compile the source code below I'm getting a linker error:
unresolved symbol int foo.
I don't understand why the symbol is unresolved.
my.h
extern int foo;
void print_foo();
void print(int);
my.cpp
#include "my.h"
#include "../../std_lib_facilities.h"
void print_foo()
{
cout<<foo<<'\n';
}
void print(int i)
{
cout<<i<<'\n';
}
use.cpp
#include "my.h"
int main(){
int foo = 7;
print_foo();
print(99);
}
This is a declaration. It tells the compiler "there's going to be an variable called
fooof typeintin the global namespace defined somewhere".This defines a local variable
fooof typeintinside the functionmain()and initializes it to 7. This local variable is not visible outside ofmain().What you didn't do is actually define
fooin the global namespace. To do that, add this line to exactly one of eithermy.cpporuse.cpp, after the includes:This defines a variable called
fooof typeintin the global namespace. (As an object of static storage duration,foois initialized to zero by default, though you can also provide a different initializer if you want.) The linker should then be able to resolve the reference to the globalfoothat's inprint_foo()when you link the object files together.It is very important that you only define
fooonce, though you can declare it as many times as you want. Doing otherwise violates the One Definition Rule and results in a linker error if you are lucky, and undefined behavior if you are not.