Generic Makefile errors

So I’ve been playing this 3D sort of 2048 game on my phone for a while now, and it’s made me want to try writing a text-based 2048 game.

I copied the c-skeleton directory to a new directory named 2048, and I think I’ve got the Makefile trimmed down to what I need. I also added minunit.h to src/ for testing purposes.

I’m starting with coding up a Cell — my name for a square of the 2048 board — so i can have a Board of an arbitrary size.

Here are my directory structure, Makefile, cell.h, cell.c, and test code. For whatever reason, the linker complains that it can’t find Cell_create when I try to run make tests.

What’s going on? I #included cell.h in the test file, and I told make that src would be used for includes

Yes, but that only takes care of the header files. You need to add the object files as arguments to the compiler for the tests.

Okay, so I updated the tests target of my Makefile so it looks like

# The Unit Tests
.PHONY: tests
tests: $(TESTS) $(OBJECTS)
        $(CC) $(SOURCES) -c
        sh ./tests/runtests.sh

So as I understand, this means that tests depends on the *_test executables — which, in turn, depend on the corresponding _tests.c file in tests/ — and the object files in src/. The rules state that the C compiler should compile the source files into .o files, but not link them. make tests still fails, which makes sense. This, I assume, is the issue, but how do I link it up correctly?

I watched the video for Ex30, just in case, but of course, Zed is focusing on testing libex29. This didn’t seem totally useful.

I guess something like this should work? You have to add the objects to the $(TESTS) target where the tests are compiled, not to the one that runs them.

$(TESTS): $(OBJECTS)

tests: $(TESTS)
    sh ./tests/runtests.sh

Still no luck, unfortunately.

If you can share the project (e.g. via GitHub) I’ll take a look.

Oh wait, I forgot the most important line above. You’ll need an explicit compile command:

$(TESTS): $(OBJECTS)
    $(CC) $(CFLAGS) -o $(patsubst %.c,%,$(@)) $@ $(OBJECTS)

Here’s the repo. Look at the cell branch to see all the code I’m currently working on.

I made a pull request.