-2

我有一个文件a.ca.h它作为支持模块,具有其他文件中的功能。

在这些函数中,我必须打开一个文件并执行读取数据、写入数据、关闭它等功能。

出于这些目的,我给出了文件指针的全局a.c声明

static FILE* pFile; 

pFile直接使用。

编译器抛出错误的错误如下:

"pFile" not defined in the function

这里有什么问题?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <arpa/inet.h>

#define path "/tmp/diag.log"

FILE* pFile;

void a(bool value) {
    if (value) {
            pFile = fopen (path,"a");
      return;
    }
    else if (!value) {
        fclose (pFile);
    }
}

void b(bool value) {
  if(value)
  {
  fprintf (pFile,"%s",message);
  }
  }

更新:错误是我的 makefile 正在从构建目录中获取文件并且更改没有得到反映。非常感谢您的帮助

4

1 回答 1

0

如果您尝试static FILE* pFile从其他编译单元中使用它,请删除static

在其他文件中添加:

 extern FILE *pfile;
于 2017-08-09T08:09:36.567 回答