Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top