How to include header files while checking a source code with splint tool?

6k views Asked by At

I created 2 C program source code files and one header file which just contains a function declaration.

mypattern.h

 #include<stdio.h>
void pattern_check(char *,int,char *);

pattern_main.c

    #include<mypattern.h>
int main(int argc,char *argv[])
{
 int i,max,flag=1;
 char *p,*cmd="end";
 if(argc!=3)
 {
  printf("usage : Invalid no of arguments\n");
  return 0;
 }
 max=atoi(argv[1]);
 char elements[max][15];
 printf("\nEnter the %d input strings :\n",max);
 for(i=0;i<max;i++)
 {
   p=elements[i];
  // printf("%u\n",p);
   scanf("%s",p);
//   printf("%s-->%u\n",p,p);
  if(*p==*cmd)
  {
   flag=0;
   max=i;
   break;
  }
 }
 if(flag)
 p=p-(sizeof(char)*((max-1)*15));
 else
 p=p-(sizeof(char)*((max)*15));
 pattern_check(p,max,argv[2]);
 return 0;
}

pattern_search.c

  #include<mypattern.h>
void pattern_check(char *elements,int max,char *pattern)
{
 int i,count=0,len=0;
 char *ptr,*ch,*pat[max],*output=NULL;
// printf("\nPattern : %s\n",pattern);
 for(i=0;i<max;i++)
 {
 // printf("\nfor loop %d\n",i);
  ptr=elements+(i*15);
  //printf("%u-->%s\n",ptr,ptr);
  ch=strstr(ptr,pattern);
  if(ch!=NULL)
  {
   pat[count]=(char *)malloc(sizeof(char));
   if(pat[count]==NULL)
   {
    printf("\nMalloc failed\n");
    return;
   }
   strcpy(pat[count],ptr);
  // printf("\nCount : %d\n",count);
  // printf("\n%s-->%u\n",pat[count],pat[count]);
  count++;
  }
 }
 printf("Pattern mateched elements :\n");
 for(i=0;i<count;i++)
 {  printf("\n-->%s\n",pat[i],pat[i]);
    len+=strlen(pat[i]);
 }
// printf("\nFinal length : %d\n",len);
  output=malloc(sizeof(char)*(len+1));
  if(NULL==output)
  {
   printf("Malloc failed \n");
   return;
  }
 for(i=count-1;i>=0;i--)
  strncat(output,pat[i],strlen(pat[i]));
 printf("\nFinal concatended string output : %s\n\n",output);
 free(output);
}

While checking this code with splint tool on linux,

 splint pattern_main.c

I am getting the following error:

pattern_main.c:1:22: Cannot find include file mypattern.h on search path:
                        /usr/include;/usr/include
  Preprocessing error. (Use -preproc to inhibit warning)
Preprocessing error for file: /user/gur29597/mysourcecode/Memory_pgm/pattern_main.c
*** Cannot continue.

How can i make it correct?

2

There are 2 answers

1
Basile Starynkevitch On BEST ANSWER

Use #include "mypattern.h" with double-quotes, not angles, and/or pass include directories with some -I arguments to gcc. And don't forget to use -Wall flag to GCC to get all warnings! To understand which headers are included, use -H or produce the preprocessed form with gcc -C -E pattern_main.c

0
another.anon.coward On

You can try either of the 2 solutions (Basile has already mentioned it but in the response it is mentioned as for gcc but it hold for splint as well):
1. Use #include "mypattern.h" instead of #include <mypattern.h>. This will prompt splint to check the header in the current directory as well.
2. Use -I<path_to_header> option with splint. Something on these lines:
splint pattern_main.c -I./ or

splint pattern_main.c -I`pwd`

You can find the details regarding adding header inclusion path for splint in their manual. Check out section 14.3 on the following link
Hope this helps!