c - 开管警告

标签 c makefile pipe fifo

在 ubuntu 12.04 LTS(64 位)上编译 makefile 时遇到以下错误:

fifo.c: In function ‘OpenPipe’:
fifo.c:28:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]
fifo.c:31:16: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
fifo.c:33:17: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
fifo.c:35:13: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
fifo.c:43:16: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
fifo.c:45:20: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
fifo.c: In function ‘ClosePipe’:
fifo.c:132:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

这是 fifo.c :

 #include "fifo.h"
 #include "out_pipe.h"
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include "stdio.h"
 #include <string.h>
 #include "typedefs.h"
 #include <sys/time.h>    
 #include <unistd.h>       
 #include "gps_sc_def.h"                     /* must be first */
 #include "clnt_buf_def.h"
 #include "s_filter.h"

 //create and open pipe. If pipe exists or can not open, return -1, else 0
 extern char PIPE;
 extern  eve_buf1 evebuf1;
 extern  eve_buf2 evebuf2;
 extern  eve_buf3 evebuf3;

 short int PIPE_COUNTER=0, PIPE_PRESC_FACTOR=1;

int OpenPipe()
   {
 char text[64], text1[64];

**28: fprintf(stderr, "Start open Pipe: %s \n",fifo1);**

  sprintf(text,"Can't create fifo ");
**31 : strcat(text, (char *)fifo0);**
  sprintf(text1,"Can't create fifo ");
 **33: strcat(text1, (char *)fifo1);**

**35:  if((mknod((char *)fifo1, S_IFIFO | PERMS,0) < 0)&&(errno != EEXIST))**
   {
  perror(text1);
  return -1;
}

 sprintf(text1,"Can't open write fifo ");
**43 :strcat(text1,(char *) fifo1);**

 **45 :if((Write = open((char *)fifo1, O_WRONLY)) == -1)**
{
  perror(text1);
  return -1;
}

 fprintf(stderr, "Pipe opened\n");
 return 0;
}

//write to pipe. if everything is OK return 0, else -1

 long write2pipe(void)
{
 ssize_t size2wr=0, sizeofeve=sizeof(evebuf1.nev)+sizeof(evebuf1.sec)+
 sizeof(evebuf1.nsec)+sizeof(evebuf1.ireg)+sizeof(evebuf1.nsca)+
 sizeof(evebuf1.nadc)+sizeof(evebuf1.ntdc)+sizeof(evebuf1.npcs);

  short i,j, *p2short;
  long buf[512], *p2long;


  //  if ((PIPE_COUNTER % PIPE_PRESC_FACTOR) != 0) return 0;
   memcpy(buf,&evebuf1.nev, sizeofeve);
  size2wr += sizeofeve;
  /* fprintf(stderr,"sizeofeve %d\n", sizeofeve); 
    fprintf(stderr,"nsca %d\n", evebuf1.nsca);
    fprintf(stderr,"nadc %d\n", evebuf1.nadc);
    fprintf(stderr,"ntdc %d\n", evebuf1.ntdc);
    fprintf(stderr,"npcs %d\n", evebuf1.npcs);
   fprintf(stderr,"n_event %d\n", evebuf2.n_event);     */
   p2long = buf+sizeofeve/4;
   p2short =(short*)(buf)+sizeofeve/2;

   if(evebuf1.nsca != 0)
   {
    for(i=0;i<evebuf1.nsca;i++)
   {
     p2long[i] = evebuf3.sca[i];
      //if(i<3)fprintf(stderr,"sca \t\t %d %x\n", i, evebuf3.sca[i]);
    }
      size2wr += 4*evebuf1.nsca;
      if(write(Write,&buf, size2wr) != size2wr)
      perror("wrong size\n");
      return 0;
     }
   else
    {
  for(i=0;i<evebuf1.nadc;i++)
{
  p2short[i] = evebuf3.adc[i];
  //fprintf(stderr,"adc \t\t %x\n", evebuf3.adc[i]);
}
  size2wr += 2*evebuf1.nadc;

  p2short += evebuf1.nadc;

  for(i=0;i<evebuf1.ntdc;i++)
{
  p2short[i] = evebuf3.tdc[i];
  //fprintf(stderr,"tdc \t\t %x\n", evebuf3.tdc[i]);
}
  size2wr += 2*evebuf1.ntdc;

  p2short += evebuf1.ntdc;

  for(i=0;i<evebuf1.npcs;i++)
{
  p2short[i] = evebuf3.pcos[i];
  //fprintf(stderr,"\t\t %x\n", evebuf3.pcos[i]);
}
  size2wr += 2*evebuf1.npcs;

  if(size2wr)
if(write(Write,&buf, size2wr) != size2wr)
     perror("wrong size\n");

  return 0;
    }
 }

//close and delete pipe. 0 --- OK, -1 crash

 int ClosePipe(){
 close(Write);
 fclose(Read);

 if(unlink((char *)fifo1)<0){
  perror("Can't unlink fifo");
  return -1;
}

return 0;
}

顺便说一下,我在 .profile 中添加了以下几行:

导出fifo0=/home/bayat/fifo0

导出fifo1=/home/bayat/fifo1

请帮我解决这些问题。 提前致谢。

最佳答案

我希望如果您查看(可能在 fifo.h 中),您会发现当您的程序时 fifo0fifo1 被定义为 int显然期望它们是 char *char[]。也许您需要的是这样的东西:

   char fifo0[] = "/home/bayat/fifo0";
   char fifo1[] = "/home/bayat/fifo1";

我认为您的 .profile 文件在这里没有任何影响。

关于c - 开管警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25677942/

相关文章:

c - 使用 getchar() 获取一行时出现无限循环

c - 为什么 FILE 指针在传递给函数后在 C 中返回 main 时会发生变化?

c - 从函数返回数组/指针

c++ - 为什么这个 makefile 总是在重新编译?

bash - 如何让 open 函数使用 bash 而不是 sh?

javascript - 如何使用 Unix 将 Node.js 脚本连接在一起 |管道(在命令行上)?

c - 通过绕道减少非决定论?

makefile - 当 makefile 规则的目标是 foreach 函数时停止出错

makefile - 将目标名称传递给 makefile 中的依赖项列表

c# - 管道运算符(operator),它是如何工作的?