0

I have a suite of helper functions defined in testhelper.c To facilitate their use I am trying to link them to a header file, testhelper.h and include this file in the testing programs I am developing. However, the files are not linking correctly, because I am getting an "undefined reference to " in the testing programs when they compile.

The code in the makefile is:

randomtestadventurer: randomtestadventurer.c dominion.o rngs.o testhelpers.o
    gcc -o randomtestadventurer -g  randomtestadventurer.c dominion.o rngs.o $(CFLAGS)

randomtestcard: randomtestcard.c dominion.o rngs.o testhelpers.o
    gcc -o randomtestcard -g  randomtestcard.c dominion.o rngs.o $(CFLAGS)

testhelpers.o: testhelpers.h testhelpers.c
    gcc -c testhelpers.c -g $(CFLAGS)

The include path in both of the randomtest programs is: #include "testhelpers.h"

This is my first attempt at a header file I've made from scratch, so I am not certain what I have done wrong.

Marshall Tigerus
  • 3,675
  • 10
  • 37
  • 67
  • You have to link testhelpers.o. Add it to the gcc command line. – PMF Feb 05 '14 at 05:54
  • See [this example of Makefile](http://stackoverflow.com/a/14180540/841108) ... Learn how to use variables in your `Makefile`. Run `make -p` to find out about builtin rules. – Basile Starynkevitch Feb 05 '14 at 05:57
  • @Basile Starynkevitch I usually clean up my makefiles further along in development. Since the makefile rules change frequently at this stage in development. I know I have a ton of redundancy that needs to go away (like both randomtests using the same set of .o files) – Marshall Tigerus Feb 05 '14 at 06:15

1 Answers1

0

The makefile that you have written seems buggy:

randomtestadventurer: randomtestadventurer.o dominion.o rngs.o testhelpers.o
    gcc -o randomtestadventurer -g  randomtestadventurer.o dominion.o rngs.o testhealpers.o $(CFLAGS)

randomtestcard: randomtestcard.o dominion.o rngs.o testhelpers.o
    gcc -o randomtestcard -g  randomtestcard.o dominion.o rngs.o testhelpers.o $(CFLAGS)

randomtestadventurer.o : randomtestadventurer.c 
    gcc -o randomtestadventurer.o -c -Wall randomtestadventurer.c header.h

randomtestcard.o : randomtestcard.c 
    gcc -o randomtestcard.o -c -Wall randomtestcard.c header.h

testhelpers.o: testhelpers.h testhelpers.c
    gcc -c testhelpers.c -g $(CFLAGS)

also you have to give rules for making dominion.o and rngs.o . In place of header.h use headers you actually included in your file