在Ubuntu中C ++程序编译失败,但在MacOSX中起作用 [英] C++ program compilation fails in Ubuntu, but works in MacOSX
问题描述
你好我有一个用C ++编写的程序。当我用g ++编译器在Mac终端中编译它时,它编译并运行。但是当我用g ++编译器在Ubuntu终端中编译相同的C ++程序时,它失败了。我不知道它为什么会发生。
Ubuntu中的g ++编译器版本是4.7.3。
是我的代码示例
#include< iostream>
使用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:函数'void putbyte(FILE *,unsigned char)':
seperate.cpp:23:21:错误:'fwrite'未在此范围内声明
seperate.cpp:函数'void putshort(FILE *,short unsigned int)':
seperate.cpp:32:21:错误:'fwrite'未在此范围内声明
seperate.cpp:函数'int putlong(FILE *,long unsigned int)':
seperate.cpp:43:28:错误:'fwrite'未在此范围内声明
seperate.cpp:在函数'short unsigned int getshort(FILE *)'中:
seperate.cpp:49:22:错误:'fread'未在此作用域中声明
seperate.cpp:在函数'long int getlong (FILE *)':
seperate.cpp:56:22:错误:'fread'未在此作用域中声明
seperate.cpp:在函数'int main(int,char **)' :
seperate.cpp:88:11:错误:'stderr'未在此范围内声明
seperate.cpp:88:69:错误:'fprin tf'未在此范围中声明
seperate.cpp:89:9:错误:'exit'未在此范围内声明
seperate.cpp:93:30:错误:'fopen'不是在此作用域中声明
seperate.cpp:95:11:错误:'stderr'未在此作用域中声明
seperate.cpp:95:61:错误:'fprintf'未在此作用域中声明
seperate.cpp:96:9:错误:'exit'未在此作用域中声明
seperate.cpp:101:22:错误:'fgetc'未在此作用域中声明
seperate.cpp:114:18:错误:'SEEK_CUR'未在此作用域中声明
seperate.cpp:114:26:错误:'fseek'未在此作用域中声明
seperate.cpp: 126:38:错误:'fread'未在此作用域中声明
seperate.cpp:131:12:错误:'fclose'未在此作用域中声明
seperate.cpp:138:25:错误:'fopen'未在此作用域中声明
seperate.cpp:141:11:错误:'stderr'未在此作用域中声明
seperate.cpp:141:54:错误:'fprintf '是n在此范围内声明
seperate.cpp:142:9:错误:'exit'未在此作用域中声明
seperate.cpp:153:36:错误:'fwrite'未在此处声明范围
seperate.cpp:162:11:错误:'stderr'未在此范围内声明
seperate.cpp:162:54:错误:'fprintf'未在此范围内声明
seperate.cpp:163:9:错误:'exit'未在此作用域中声明
seperate.cpp:174:36:错误:'fwrite'未在此作用域中声明
seperate.cpp :183:11:错误:'stderr'未在此作用域中声明
seperate.cpp:183:54:错误:'fprintf'未在此作用域中声明
seperate.cpp:184:9 :错误:'exit'未在此范围中声明
seperate.cpp:195:36:错误:'fwrite'未在此范围内声明
dfo @ ubuntu:〜/ Desktop / abc-master $ g ++ -v
您需要包含 stdio.h 用于
fwrite
和 FILE
。
#include< stdio.h>
该标准允许标题包含其他标题,但不能依赖这些间接包含。您需要明确包含您打算使用的每个标题。
Hi I've a program written in C++. When I compile it in Mac terminal with g++ compiler, it compiles and runs. But when I compile the same C++ program in Ubuntu terminal with g++ compiler, it Fails. I don't know why it happens.
g++ compiler version in Ubuntu is 4.7.3.
Here is my code sample
#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);
}
I get following error
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
You need to include stdio.h
for fwrite
and FILE
.
#include <stdio.h>
The standard allows headers to include other headers, but you cannot rely on these indirect includes. You need to explicitly include every header you intend to use.
这篇关于在Ubuntu中C ++程序编译失败,但在MacOSX中起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!