undefined reference to 'function'

All,

I know this topic comes up often, but the solutions I've seen don't seem applicable in my case.
I am building on Linux (though I saw the same issue on Windows with Dev-C++ and Code::Blocks).
There are two source files, a header and a makefile.
All four files reside in the same directory.
I am compiling using g++ on an HP PC with Ubuntu 11.10.
The output follows the makefile.
Thank you for looking!

stack.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef STACK_H
#define STACK_H 

const int STACK_MAX_SIZE = 10;

class stack
{
    private:
        int count;                   //  Tracks number of items in stack.
        int data[ STACK_MAX_SIZE ];  //  Stack buffer.

    public:
        void init(        void  );   //  Clears the counter.
        void push(    int value );   //  Adds a value to the stack.
        int  pop (        void  );   //  Returns a value from the stack. 
        void print_stack( void  );   //  Prints all values currently in stack.
};
#endif  //  STACK_H 


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

void stack::init(     void  ) { count = 0; } 
void stack::push( int value ) { data[ count ] = value;      ++count; }
int  stack::pop (     void  ) { --count;    return( data[ count ] ); }

void stack::print_stack( void ) 
{
    for( int i = 0;   i < count;   ++i )
    {
        std::cout << "i=" << i << "  value=" << data[ i ] << "\n";
    }
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
#include "stack.h"

int main( int argc, char * argv[] )
{
    stack st_obj;

    st_obj.init();
    st_obj.push( 4 );
    st_obj.print_stack();

    return( 0 );
}


Makefile:
1
2
3
4
5
6
7
8
a.out : stack.o main.o
        g++ -o a.out main.o stack.o

main.o : main.cpp
        g++ -c main.cpp

stack.o : 
        g++ -c stack.cpp 


Errors:

g++ -c main.cpp
g++ -o a.out stack.o main.o 
main.o: In function 'main':
main.cpp:(.text+0x17): undefined reference to 'stack::init()'
main.cpp:(.text+0x28): undefined reference to 'stack::push(int)'
main.cpp:(.text+0x34): undefined reference to 'stack::print_stack()'
collect2: ld returned 1 exit status
make  *** [a.out] Error 1

Last edited on
First, use tabs instead of spaces in the makefile.

Your main.cpp has a typo in line 7, which should cause a compile error.

Also, you didn't put the dependencies.
As a guess, you do have a `stack.o' from a previous version, that does not contain those definitions.

Make a clean `rm *.o' and try again
Thank you ne555,

I removed the *.o files and voila it built.

What did you mean by "didn't put the dependencies"?
I assume you're talking about the makefile, but I must be missing something?

Regarding the tabs and typo, my Linux PC is not on the network, so I had to manually retype everything. The original files are fine, so I'm sorry but I am impressed by your discerning eye!
Yes, the dependencies in the makefile
1
2
3
4
5
6
7
8
a.out : stack.o main.o
	g++ -o a.out main.o stack.o

main.o : main.cpp stack.h
	g++ -c main.cpp

stack.o : stack.cpp stack.h
	g++ -c stack.cpp
There we are saying that if you change `stack.cpp' you'll need to recompile it to create `stack.o'
If you change `stack.h' you need to recompile `main.cpp' and `stack.cpp', as they both include it, and replace the old *.o
Last edited on
Topic archived. No new replies allowed.