Reputation: 117
I have three files:
my.cpp
#include "my.h"
#include <iostream>
void print_foo() {
cout << foo << '\n';
}
void print(int i) {
cout << i << '\n';
}
my.h
extern int foo;
void print_foo();
void print(int);
use.cpp
#include "my.h"
int main(int argc, char *argv[]) {
foo = 7;
print_foo();
print(99);
return 0;
}
Now when I run g++ my.cpp use.cpp
I get the error
/usr/bin/ld: /tmp/ccUKJUlZ.o: in function `print_foo()':
my.cpp:(.text+0x6): undefined reference to `foo'
/usr/bin/ld: /tmp/ccN0mIhY.o: in function `main':
use.cpp:(.text+0x11): undefined reference to `foo'
collect2: error: ld returned 1 exit status
Additionally, if I run g++ -c my.cpp
everything goes alright, but, if I then run g++ my.o use.cpp
I get the same error.
Upvotes: 2
Views: 126
Reputation: 416
You never actually define a variable foo
- in both use.cpp
and my.cpp
, you use foo
, and in my.h
you declare it as an extern
.
See the beginning of this response for more information on declaring vs. defining. You may think that your problem would be solved if you added a type in front of your foo = 7
line in use.cpp
; however, what you also need to do is make foo
a global variable instead of a local one (which it is when you declare it simply within main
), as extern
will only "find" variables that have global scope. You can make a variable global by declaring it outside of any function (side note - you should only use global variables when you absolutely have to).
Therefore, you could solve your problem by changing your use.cpp
to the following:
#include "my.h"
int foo = 7;
int main(int argc, char *argv[]) {
print_foo();
print(99);
return 0;
}
Upvotes: 1