0

I have a project using functions from a class CRandomMersenne, declared in header randomc.h; the functions are defined in a different file mersenne.cpp. In my makefile I have an object *MC_funcs2.o* that uses functions from the class. The source *MC_funcs2.cpp* includes the header randomc.h. However the compiler complains:

MC_funcs2.o:MC_funcs2.cpp:(.text+0x20): undefined reference to `CRandomMersenne::Random()'

I understand there is something I've done wrong with declaring functions outside the class definition, including header file to use mentioned functions and maybe with the linking in the makefile. Here is a stripped down version of some of the files:

makefile:

SpMC3: SpMC3.cpp SpMC.h mersenne.o MC_funcs2.o
     g++ SpMC3.cpp MC_funcs2.o mersenne.o -o SpMC3

MC_funcs2.o: MC_funcs2.cpp SpMC.h randomc.h
     g++ -c MC_funcs2.cpp mersenne.cpp

mersenne.o: mersenne.cpp randomc.h userintf.cpp
     g++ -c mersenne.cpp userintf.cpp

SpMC3.cpp (main program):

#include "SpMC.h"

int main() {
     cout << "boing" << endl;
     return 0;
}

MCfuncs2.cpp (the one that doesn't compile):

#include "SpMC.h"
#include "randomc.h"

CRandomMersenne RanGen(time(0));

void outrandom() {
     ofstream out;
     out << RanGen.Random() << endl;
     return;
}

Any ideas?

jorgen
  • 3,425
  • 4
  • 31
  • 53
  • Ooops I found my mistake, in the makefile the first line of MCfuncs2.o should also have mersenne.cpp. How do I delete this question? – jorgen Jan 27 '13 at 16:49

2 Answers2

1

I would strongly advise compiling each individual source file *.c to a object file *.o, i.ex. mersenne.cpp -> mersenne.o. This can be achieved without having to specify each object file manually.

CC = gcc
CFLAGS = -g -O2
OBJECTS = main.o foo.o

main : $(OBJECTS)
    $(CC) $(CFLAGS) $(OBJECTS) -o main

%.o : %.c
    $(CC) $(CFLAGS) -c $<

For further information on makefiles, please take a look a this tutorial or excellent SO answer in How to make a SIMPLE C++ Makefile?.

Community
  • 1
  • 1
harpun
  • 4,022
  • 1
  • 36
  • 40
1

This error just means mersenne.cpp not being compiled ...make sure it get compiled and linked...

Saqlain
  • 17,490
  • 4
  • 27
  • 33