Note: this could be considered as a duplicate of Gcov is not generating *.gcda file, however :
- This question has no answer
- I don't think the OP has the same system than me, so I don't think it'll be ok for me to just go and edit his question. Moreover, the question is 5yo.
- I produce here a "minimal" and reproducible example.
So, I was trying to reproduce an error that I had with gcov...and I run on another instead. Here are some files:
main.c:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "modeAlpha.c"
//~ #include "modeBeta.c"
//~ #include "modeGamma.c"
int main(){
char MyText[256];
printf("Hello! This is a gcov test. Enter some word here: ");
scanf("%s", MyText);
printf("This is your text: %s\n",MyText);
mode_write_Alpha(MyText);
//~ mode_write_Beta(MyText);
mode_wait_Alpha();
//~ mode_free_Beta(MyText);
//~ mode_end_Beta();
}
modeAlpha.c:
#ifdef modeAlpha
#include "modeAlpha.h"
#include "AlphaFunctions.c"
#endif
modeAlpha.h:
#ifdef mode_write
#undef mode_write
#endif
#define mode_write mode_write_Alpha
#ifdef mode_wait
#undef mode_wait
#endif
#define mode_wait mode_wait_Alpha
#ifdef mode_free
#undef mode_free
#endif
#define mode_free mode_free_Alpha
#ifdef mode_end
#undef mode_end
#endif
#define mode_end mode_end_Alpha
#include "modes.h"
AlphaFunctions.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "modes.h"
void mode_write(char* string){
printf("Alpha Writing what you said: %s\n", string);
}
void mode_wait(){
printf("Alpha Waiting to process\n");
}
void mode_free(char* string){
printf("Alpha Free memory\n");
free(string);
}
void mode_end(){
printf("This is the end of Alpha\n");
}
modes.h:
void mode_write(char* test);
void mode_wait(void);
void mode_free(char* test);
void mode_end(void);
And the Makefile:
GCOVOPT= -std=c99 --coverage -fprofile-arcs -ftest-coverage -fprofile-generate
LDLIBS= -lpthread -lgcov
LDFLAGS= -Wall -pedantic -g -g3 -ggdb $(GCOVOPT)
CFLAGS= -c -Wall -pedantic -g -g3 -ggdb
OBJS=MyProg.o modeAlpha.o
SRCS=main.c modeAlpha.c
MODEALPHA=modeAlpha
MODEBETA=modeBeta
MODEGAMMA=modeGamma
MyProg: $(OBJS)
gcc $(OBJS) $(LDFLAGS) $(LDLIBS) -o MyProg
MyProg.o: main.c
gcc -c $< -o $@ $(LDFLAGS) $(LDLIBS) -D$(MODEALPHA)
modeAlpha.o: modeAlpha.c
gcc -c $< -o $@ $(CFLAGS) $(LDLIBS)
Now I know those files may feel weirds and the code is far from perfect, but remember I was trying to reproduce another error. Still, this is working well, generating .gcno file after compiling and then .gcda file after execution of the prog.
The problem appears when I add a "modeBeta". I will simply have a modeBeta.c, BetaFunctions.c and modeBeta.h, all exactly the same as the ones I gave here except that all occurences of the word "Alpha" are replaced with "Beta". You can see some commented lines in the main, related to this modeBeta thing; I uncomment them. And finally changed the makefile like this:
Makefile:
LDLIBS= -lpthread -lgcov
LDFLAGS= -Wall -pedantic -g -g3 -ggdb $(GCOVOPT)
CFLAGS= -c -Wall -pedantic -g -g3 -ggdb
OBJS=MyProg.o modeAlpha.o modeBeta.o
SRCS=main.c modeAlpha.c modeBeta.c
MODEALPHA=modeAlpha
MODEBETA=modeBeta
MODEGAMMA=modeGamma
MyProg: $(OBJS)
gcc $(OBJS) $(LDFLAGS) $(LDLIBS) -o MyProg
MyProg.o: main.c
gcc -c $< -o $@ $(LDFLAGS) $(LDLIBS) -D$(MODEALPHA) -D$(MODEBETA)
modeAlpha.o: modeAlpha.c
gcc -c $< -o $@ $(CFLAGS) $(LDLIBS)
modeBeta.o: modeBeta.c
gcc -c $< -o $@ $(CFLAGS) $(LDLIBS)
I still got the .gcno file, but no more .gcda after executing the prog. Note that if I don't call for any mode_XXX_Beta function in the main (like, if I don't uncomment the commented lines in main.c), then the gcda file is produced just as it should.
Any idea of what's going on here??
OS: i'm running on Win10 64bits with MinGW (winLib repo) and gcc 10.1.0.
I found out the problem: mode_free_Beta was freeing a not-allocated memory, which resulted in the end of the prog without any message error. So I thought it was running well, but didn't paid attention to the fact that I never reach mode_end_Beta.
Program ending unnaturally = no gcda file.
In this case, it can be corrected just with
If it happens to you, just make sure that your prog is running well until the end ;)