Enabling Code Coverage for Makefile projects

If you're using a non-QNX build environment in which you write your own Makefile, you have to manually pass the coverage option to the compiler.

Code coverage instrumentation uses a signal to tell the program to deliver its data to the IDE. Depending on your design, the Code Coverage tool poses several risks:
  • It can modify or break the behavior of programs being monitored.
  • It can cause code to run even though the test suite does not actually execute it.
  • It can result in data not actually being collected at all.
To enable code coverage for Makefile projects:
For qcc or gcc, compile and link with these options: -fprofile-arcs -ftest-coverage
For example, in the Makefile, you'll have the following variables set:
CFLAGS += -g -fprofile-arcs -ftest-coverage 
LDFLAGS+= -g -fprofile-arcs -ftest-coverage
Your Makefile might look something like the one shown here, which is taken from the sample code coverage project included with the IDE but contains additional comments:
DEBUG = -g 
CC = qcc 
LD = qcc 
CFLAGS += -Vgcc_ntox86 $(DEBUG) -c -Wc,-Wall -I. -O0 -Wc,-ftest-coverage 
LDFLAGS+= -Vgcc_ntox86 $(DEBUG) -ftest-coverage -fprofile-arcs  

# CC refers to the program for compiling C programs (the default is qcc).
# Use CXX as the program for compiling C++ programs. 
# CFLAGS are additional flags to give to the C compiler. Use CFLAGS for
# the C++ compiler. 

# -c compiles or assemble the source files, but doesn't link, and the -Wc
# captures the warning messages. The linking stage isn't done. The ultimate 
# output is in the form of an object file for each source file. 

# -Wall turns on all optional warnings that are desirable for normal code.
# -I adds the current directory to the list of directories to search for  
# header files. Directories named by -I are searched before the standard  
# system include directories. 

# -O0 is an optimization flag that indicates 'Do not optimize.' 

# LDFLAGS are additional flags to give to compilers when they invoke 
# the ld linker. 

# -ftest-coverage -Wc means that Code Coverage is enabled for your project,
# and the data is used for test coverage analysis. 

# -fprofile-arcs adds code so that program flow arcs are instrumented.  
# During execution, the program records how many times each branch and call 
# is executed and how many times it is taken or returns, and it saves this 
# data to a file with the extension .gcda for each source file.
# For Code Coverage, you'll need the -fprofile-arcs -ftest-coverage options
# in both the compile and link lines.

dir := $(shell pwd) 

BINS = rbt_client rbt_server 

# This next line is the rule for <cmd>all</cmd> that  
# incrementally builds your system by performing a <cmd>make</cmd>  
# of all the top-level targets the Makefile knows about. It does this by 
# expressing a dependency on the results of that system, which in turn  
# have their own rules and dependencies. 

all: $(BINS) 

# The following line shows a simple rule for cleaning your build  
# environment. It cleans your build environment by deleting all files  
# that are normally created by running make. 

# It has a Target named <cmd>clean</cmd> to left of the colon,  
# no dependencies (to the right of the colon), and two commands that are 
# indented by tabs on the lines that follow. 

rm -f *.o *.img *.gcno *.gcda $(BINS) 

# The following lines are Dependency Rules, which are rules without any  
# command. If any file to the right of the colon changes, the target to  
# the left of the colon is no longer considered current (out of date). 
# Dependency Rules are often used to capture header file dependencies. 

rbt_server: rbt_server.o 

# Alternatively, to manually capture dependencies, several automated 
# dependency generators exist. 

rbt_server.o : rbt_server.c rbt_server.h 
$(CC) $(CFLAGS) $(dir)/$< 

rbt_client: rbt_client.o 

rbt_client.o: rbt_client.c rbt_server.h 
$(CC) $(CFLAGS) $(dir)/$<