0

I'm creating a templated "Heap" class, and I have the following structure of a directory:

--HeapFolder
|- Heap.cpp
|- Heap.h
|- Heap_Testbench.cpp

Here, the Heap.h file is the outline of the templated class and Heap.cpp implements it.

What I want to do is to be able to write tests and compile it using a Makefile. What I found is that in the Heap_Testbench.cpp file, I needed to include both the .cpp and the .h files in order to avoid linker errors.

Even though I have the following Makefile:

heap_test: Heap.o Heap_TestBench.o
    g++ -o $@ $^ -std=c++11 -lboost_unit_test_framework

Heap.o: Heap.cpp Heap.h
    g++ -std=c++11 -o $@ $< -c

Heap_TestBench.o: Heap_TestBench.cpp
    g++ -std=c++11 -DBOOST_TEST_DYN_LINK -o $@ $< -c

clean:
    rm *.o

I noticed that the implementation wasn't being linked to the "heap_test" binary (which lead to linker errors when I tried to call any function in the Heap class). Why is this the case? More specifically, why would one need to include the ".cpp" file in the "Heap_Testbench.cpp" if I'm already linking it with "Heap.o"?

Ricardo Iglesias
  • 595
  • 2
  • 6
  • 16

0 Answers0