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
constint 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";
}
}
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
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!
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