April 14, 2015

script to detected unused C files

If you have a large project with a lot of C files, once you compiled them, you will end up with a lot of .o files. Then you can use the .o files to find out which .c files is not used (conditionally compiled/not compiled). To do this, follow the steps below:

1. cd subdir
2. ls -1 > ../1.out
3. gawk -f lsnu.awk ../1.out

The lsnu.awk script
{
        if ($1 ~ /\.c$/){
                cfiles[$1]=1;
                print $1;
                next;
        }
        if ($1 ~ /\.o$/){
                ofiles[$1]=1;
                #printf("%s\n",$1);
                next;
        }

}

END{
        printf("-------------\n");
        for ( i in  cfiles){
                f=i;
                gsub(/\.c$/,".o",i);
                if (ofiles[i]!=1){
                        printf("%s ",f);
                        }
        }
        printf("\n");
}

No comments:

Post a Comment