【问题标题】:How to include header files while checking a source code with splint tool?如何在使用夹板工具检查源代码时包含头文件?
【发布时间】:2011-11-15 07:18:46
【问题描述】:

我创建了 2 个 C 程序源代码文件和一个仅包含函数声明的头文件。

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);
}

在 linux 上使用夹板工具检查此代码时,

 splint pattern_main.c

我收到以下错误:

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.

我怎样才能使它正确?

【问题讨论】:

    标签: linux parsing splint


    【解决方案1】:

    您可以尝试两种解决方案中的任何一种(Basile 已经提到它,但在回复中提到了gcc,但它也适用于splint):
    1. 使用#include "mypattern.h" 而不是#include &lt;mypattern.h&gt;。这将提示splint 也检查当前目录中的标题。
    2. 使用-I&lt;path_to_header&gt; 选项和splint。以下几行:
    splint pattern_main.c -I./

    splint pattern_main.c -I`pwd`
    

    您可以在他们的手册中找到有关为splint 添加标头包含路径的详细信息。查看以下link
    的第 14.3 节 希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      使用带有双引号的#include "mypattern.h",而不是角度,和/或将带有一些-I 参数的包含目录传递给gcc。并且不要忘记对 GCC 使用 -Wall 标志来获取所有警告!要了解包含哪些标题,请使用-H 或使用gcc -C -E pattern_main.c 生成预处理表单

      【讨论】:

      • 这个通知可能是由于另一个问题吗?我的代码是为了交叉编译而不是在我的 x86 linux 开发框下构建的,但我仍然需要对其进行静态检查。
      猜你喜欢
      • 2022-10-20
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      • 2014-10-29
      • 2020-06-17
      • 2016-11-15
      相关资源
      最近更新 更多