1

嗨,我有一个用 C++ 编写的程序。当我使用 g++ 编译器在 Mac 终端中编译它时,它会编译并运行。但是当我使用 g++ 编译器在 Ubuntu 终端中编译相同的 C++ 程序时,它会失败。我不知道为什么会这样。

Ubuntu 中的 g++ 编译器版本为 4.7.3。

这是我的代码示例

#include <iostream>

using namespace std;

#define IXSIZE 400
#define IYSIZE 400
#define IZSIZE 3

void putbyte(FILE *outf, unsigned char val)
{ 
    unsigned char buf[1]; 

    buf[0] = val; 
    fwrite(buf,1,1,outf); 
} 

void putshort(FILE *outf, unsigned short val) 
{ 
    unsigned char buf[2]; 

    buf[0] = (val>>8); 
    buf[1] = (val>>0); 
    fwrite(buf,2,1,outf); 
} 

我收到以下错误

seperate.cpp: In function ‘void putbyte(FILE*, unsigned char)’: seperate.cpp:23:21: error: ‘fwrite’ was not declared in this scope seperate.cpp: In function ‘void putshort(FILE*, short unsigned int)’: seperate.cpp:32:21: error: ‘fwrite’ was not declared in this scope seperate.cpp: In function ‘int putlong(FILE*, long unsigned int)’: seperate.cpp:43:28: error: ‘fwrite’ was not declared in this scope seperate.cpp: In function ‘short unsigned int getshort(FILE*)’: seperate.cpp:49:22: error: ‘fread’ was not declared in this scope seperate.cpp: In function ‘long int getlong(FILE*)’: seperate.cpp:56:22: error: ‘fread’ was not declared in this scope seperate.cpp: In function ‘int main(int, char**)’: seperate.cpp:88:11: error: ‘stderr’ was not declared in this scope seperate.cpp:88:69: error: ‘fprintf’ was not declared in this scope seperate.cpp:89:9: error: ‘exit’ was not declared in this scope seperate.cpp:93:30: error: ‘fopen’ was not declared in this scope seperate.cpp:95:11: error: ‘stderr’ was not declared in this scope seperate.cpp:95:61: error: ‘fprintf’ was not declared in this scope seperate.cpp:96:9: error: ‘exit’ was not declared in this scope seperate.cpp:101:22: error: ‘fgetc’ was not declared in this scope seperate.cpp:114:18: error: ‘SEEK_CUR’ was not declared in this scope seperate.cpp:114:26: error: ‘fseek’ was not declared in this scope seperate.cpp:126:38: error: ‘fread’ was not declared in this scope seperate.cpp:131:12: error: ‘fclose’ was not declared in this scope seperate.cpp:138:25: error: ‘fopen’ was not declared in this scope seperate.cpp:141:11: error: ‘stderr’ was not declared in this scope seperate.cpp:141:54: error: ‘fprintf’ was not declared in this scope seperate.cpp:142:9: error: ‘exit’ was not declared in this scope seperate.cpp:153:36: error: ‘fwrite’ was not declared in this scope seperate.cpp:162:11: error: ‘stderr’ was not declared in this scope seperate.cpp:162:54: error: ‘fprintf’ was not declared in this scope seperate.cpp:163:9: error: ‘exit’ was not declared in this scope seperate.cpp:174:36: error: ‘fwrite’ was not declared in this scope seperate.cpp:183:11: error: ‘stderr’ was not declared in this scope seperate.cpp:183:54: error: ‘fprintf’ was not declared in this scope seperate.cpp:184:9: error: ‘exit’ was not declared in this scope seperate.cpp:195:36: error: ‘fwrite’ was not declared in this scope dfo@ubuntu:~/Desktop/abc-master$ g++ -v

4

1 回答 1

6

您需要包括stdio.hforfwriteFILE

#include <stdio.h>

该标准允许标头包含其他标头,但您不能依赖这些间接包含。您需要明确包含您打算使用的每个标头。

于 2014-02-22T11:31:12.387 回答