tags:

views:

61

answers:

1

I think I have a problem with my makefile. I'm writing this program:

  • Q2.cpp contains the main.
  • Agent.cpp Agent.h
  • Timing.cpp Timing.h
  • RandomDouble.cpp RandomDouble.cpp

And I'm using the header randoma.h in RandomDouble.cpp. I downloaded the randomaelf64.a file and I wrote this makefile:

 Q2 : Q2.o Agent.o Timing.o RandomDouble.o
     g++ -Wall -g randomaelf64.a RandomDouble.o Q2.o Agent.o Timing.o -o Q2

 Q2.o : Q2.cpp Agent.h Timing.h
     g++ -Wall -g -c Q2.cpp -o Q2.o

 Agent.o : Agent.cpp Agent.h Timing.h RandomDouble.h PrintQ2.h
     g++ -Wall -g -c Agent.cpp -o Agent.o

 RandomDouble.o : RandomDouble.cpp RandomDouble.h  randoma.h
     g++ -Wall -g -c RandomDouble.cpp -o RandomDouble.o

 Timing.o : Timing.cpp Timing.h Agent.h
     g++ -Wall -g -c Timing.cpp -o Timing.o

 clear :
     rm *.o Q2

Except for the first command, each g++.. command is working when I do it separately. Even when I add a main() to RandomDouble.cpp and run:

g++ -Wall -g randomael64.a RandomDouble.cpp -o rand

it's working. So I think that maybe the problem is with my makefile.

When I run make I get this error:

RandomDouble.o: In function `InitSeed()':
/cs/stud/ofrenk33/CPP/ex1/RandomDouble.cpp:11: undefined reference to `MersenneRandomInit'
RandomDouble.o: In function `InitSeed(int)':
/cs/stud/ofrenk33/CPP/ex1/RandomDouble.cpp:16: undefined reference to `MersenneRandomInit'
RandomDouble.o: In function `GetRandomDouble()':
/cs/stud/ofrenk33/CPP/ex1/RandomDouble.cpp:21: undefined reference to `MersenneRandom'
Agent.o: In function `Agent::SendMessage()':
/cs/stud/ofrenk33/CPP/ex1/Agent.cpp:31: undefined reference to
...
make: *** [Q2] Error 1

I need to say that there are functions declared in randoma.h which are in the randomaelf64.a library.

How do I fix this problem?

+5  A: 

You need to link against the library after all your own object files. The linker will only include as much of the library as it needs, and if there are no unresolved references yet, then none of the library will be needed. Change the first rule to:

Q2 : Q2.o Agent.o Timing.o RandomDouble.o 
    g++ -Wall -g RandomDouble.o Q2.o Agent.o Timing.o randomaelf64.a -o Q2
Mike Seymour