PDA

View Full Version : Problem linking files in C++



Feenaboccles
03-25-2002, 11:57 AM
I wrote a simple bit array class declared in BitArray.h and defined in BitArray.cpp. I used G++ 2.95.3 20010315 (SuSE) to compile it. When compiled with -DDEBUG a small main function was also compiled, and everything was fine.

However when I use it in programs written in other files, the linker can't find any of the methods, e.g.

[test.cpp]
-------------------------------------
#include "BitArray.h"
#include <stdlib.h>
#include <iostream.h>

int main (int argc, char** argv)
{
BitArray b(1024); // 1024 bit array

// print bit (0 or 1) at index 0
cout << b.bitAt (0) << endl;

// find index of first occurence of bit
// 1 starting at index 0
cout << b.findBit (0, 1) << endl;

return EXIT_SUCCESS;
}

When I compile with the following
g++ -c BitArray.cpp
g++ test.cpp BitArray.o

I get:
/tmp/cc31oTBq.o: In function `main':
/tmp/cc31oTBq.o(.text+0x2f): undefined reference to `BitArray::bitAt(unsigned int) const'
collect2: ld returned 1 exit status

However the exact same code in the main function in BitArray.cpp works.

Can anyone tell me what's going on?!!

SilentStrike
03-27-2002, 01:16 AM
Dunno... if you post the code I'll try to help you get it to link though.

kwigibo
03-29-2002, 11:41 PM
Do you use a constructor?

Feenaboccles
04-05-2002, 08:48 AM
Thanks lads, but in the end I just re-wrote it in Java. There were too many weird problems.