Compilation / Linking Problem for very simple program

Hello people,
I am a complete newbie to programming, and I am having some trouble completing a very simple drill in Stroustrups Book Programming Principles and Practice using C++.
I have 3 very simple program parts, my.h , my.cpp, and use.cpp. This is how each of them looks:

my.h
1
2
3
extern int foo;
void print_foo();
void print(int);

my.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include "my.h"

using namespace std;

void print_foo()
{
	cout <<"\n"<<foo<<"\n";
}

void print(int i)
{
	cout <<"\n"<<i<<"\n";
}

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

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


Now, when I try to compile using g++ use.cpp, I first get two problems. Firstly, I get a message saying undefined reference to foo in function print_foo(). I'm not really sure how I should be doing this instead. In the book it says I should use main in use.cpp to set the value of foo to 7. Does defining foo within main not set its value for foo everywhere?
Also, I am having problems with undefined reference to print_foo() and print(int). According to the book, my.h should contain exactly what is given above, so is this a problem with the way I am compiling? (am running Ubuntu, using g++ compiler)
Even when I stuff all the code into one file, like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>

using namespace std;

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

void print(int i)
{
	cout << "\n" << i << "\n";
}

int main()
{
	int foo = 10;
	print_foo();
	print(99);
	return 0;
}

void print_foo()
{
	cout << "\n" << foo << "\n";
}

I get the undefined reference to foo error. I am really confused here, and would greatly appreceate any help in showing / explaining to me what I am doing wrong here.
Thank you in advance!!!
Last edited on
I should use main in use.cpp to set the value of foo to 7. Does defining foo within main not set its value for foo everywhere?

It would, but you're not doing that. You only have a local variable called foo in main().
Hi Athar,
I am probably being completely dense here, but I don't see any other way of defining foo from within main. I mean if I just had a global variable, and defined it to be 10, that would solve the problem, is that correct? But the drill specifically says I should only set the value of foo from within main. How can I accomplish this and not have it be a local variable?
okay, I got the second version to compile, by removing the extern keyword and then removing int from the definition of foo in main. However, I still can't get the linked version to compile. Keep getting undefined reference errors for foo, print_foo, and print...would appreceate some help here. Thanks!
I don't see any other way of defining foo from within main. I mean if I just had a global variable, and defined it to be 10, that would solve the problem, is that correct?

You declared a global variable called foo, so yes, that's what you need to define. Note that you're initializing the variable to 10, you're not defining it to be 10 - foo is not a constant.

But the drill specifically says I should only set the value of foo from within main.

Assigning a value is different from defining an object. An assignment is done with foo=10; and you can do it as often as you want.

Keep getting undefined reference errors for foo, print_foo, and print...would appreceate some help here.

You need to link both translation units, using e.g. g++ my.cpp use.cpp -o parts
Perfect! Thank you so much. I wasn't aware of that particular sytax for g++. Also, thanks for clearing up the assignment vs. definition issue.
woodchuck, I am getting the same "undefined reference error" as you.
My 3 files look like this:

my.h
1
2
3
extern int foo;
void print_foo();
void print(int);


my.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "my.h"
#include <iostream>
#include <string>

using namespace std;

void print_foo() 
{
	cout << foo << "\n";
}

void print(int i) 
{
	cout << i << "\n";
}


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

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


I am using:
g++ use.cpp my.cpp -o drill

but I am getting an error that says:
/tmp/ccpaRAJK.o: In function `main':
use.cpp:(.text+0x6): undefined reference to `foo'
/tmp/cc4JD3xz.o: In function `print_foo()':
my.cpp:(.text+0x6): undefined reference to `foo'
collect2: ld returned 1 exit status


Did you manage to get the "linked version" to compile? If so, how?
Did you manage to get the "linked version" to compile? If so, how?

By defining a global int variable called foo in either use.cpp or my.cpp.
Thank you, Athar. Now I understand the difference between declaration and definition.
Topic archived. No new replies allowed.