HKUSTMultimedia Technology Research Center
15 Mar 2018

C++ Notes

Valgrind for Memory Debugging

Perform full memory leak investigation:

valgrind –leak-check=full ./a.out


Generic Programming with Separate Compilation

Class template is just a series of instructions to generate a class with particular data types specified during the compile time. The main.cpp includes the header file of the class template (template.h) but it does not include the implementation of the template (template.cpp). Hence, the compiler cannot link them together during compilation. There are three ways to solve this:

  • Put all implementations inside the header file (recommended)
  • Include the implementation file of the template at the end of the header
  • Instantiate the classes with the data types that will be used in the main.cpp at the end of the header file



Making use of makefile allows separate compilation and multiple main.cpp’s in the sample program. Executing the following makefile generates 2 executables, main_a.exe and main_b.exe. Make sure that you are opening makefile project in Eclipse.

all: main_a.exe main_b.exe

main_a.exe: main_a.cpp A.o B.o
g++ -std=c++11 -o main_a.exe main_a.cpp A.o B.o

main_b.exe: main_b.cpp B.o C.o
g++ -std=c++11 -o main_b.exe main_b.cpp B.o C.o

A.o: A.cpp A.h
g++ -std=c++11 -c A.cpp

B.o: B.cpp B.h
g++ -std=c++11 -c B.cpp

C.o: C.cpp C.h
g++ -std=c++11 -c C.cpp

rm -f *.o *.exe

Programming Language Leave a comment

Leave a Reply