【问题标题】:C++ program compilation fails in Ubuntu, but works in MacOSXC++ 程序编译在 Ubuntu 中失败,但在 MacOS 中有效
【发布时间】:2014-02-22 11:24:58
【问题描述】:

您好,我有一个用 C++ 编写的程序。当我使用 g++ 编译器在 Mac 终端中编译它时,它会编译并运行。但是当我在 Ubuntu 终端中使用 g++ 编译器编译相同的 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

【问题讨论】:

  • 代码。我们需要查看您的代码。
  • 请贴出代码。没有它,我们什么也做不了;我们只能猜测问题可能是什么。
  • 请发布一个最小的测试用例、编译器调用和错误。

标签: c++ macos ubuntu g++ g++-4.7


【解决方案1】:

您需要为fwriteFILE 添加stdio.h

#include <stdio.h>

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

【讨论】:

  • @aqavi_paracha 例如可以被iostream包含。正如我在回答中所说,您不能依赖这种行为。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-26
相关资源
最近更新 更多