On 2013-12-18, suse noob13 <suse_noob13@no-mx.forums.opensuse.org> wrote:
>
> Thanks for your responses.
> First of all, my system is openSUSE 13.1 (Bottle, i586), Kernel is
> 3.11.6-4-desktop)
Thank you. This would mean your gcc is up to date.
> Ok, I’m using g++ and including some ilbc headers for certain
> operations. Hence C++ is relevant.
So you are using the standard C header libc for a C++ compile? Generally, mixing code is a bad idea. If you want call C
functions from C++ compilers, it’s much safer to pre-compile the C code and link to the compiled binaries to call it
from C++. You shouldn’t include stdio.h in any C++ code and use the C++ equivalent (i.e. iostream.h).
> C++11 officially declared *getc *deprecated and quite a few libraries
> started throwing an error when this function is referenced. When the
> function is defined, it messes up some #*ifdefs *and compilation fails
> in the end.
I suspect that gcc has more than one version of getc() - see below.
> When I get the latest *glibc
> *(http://ftp.heanet.ie/mirrors/gnu/libc/glibc-2.18.tar.gz), *getc *is
> NOT present in stdio.h. The only entry of *getc is:
>
> extern int __getc_unlocked(FILE __fp);
>
> So I don’t know which version I got with my SUSE, even though
> gnu_get_libc_version() tells me “2.18” 
Consider the trivial code (called `a.c’):
# include <stdio.h>
int main(int argc, char* argv]) {
int c;
int count;
FILE *stri = fopen("a.c", "r");
while( c != EOF ) {
c = getc(stri);
if (c == 'A') count++;
}
fclose(stri);
printf ("File contains %dA.
", count);
return 0;
}
This compiles and runs fine:
sh-4.2$ gcc a.c
sh-4.2$ ./a.out
File contains 2A.
sh-4.2$
If I delete or comment out the # include <stdio.h> line, the result is:
sh-4.2$ gcc a.c
a.c: In function ‘main’:
a.c:6:2: error: unknown type name ‘FILE’
FILE *stri = fopen("a.c", "r");
^
a.c:6:15: warning: initialization makes pointer from integer without a cast [enabled by default]
FILE *stri = fopen("a.c", "r");
^
a.c:7:14: error: ‘EOF’ undeclared (first use in this function)
while( c != EOF ) {
^
a.c:7:14: note: each undeclared identifier is reported only once for each function it appears in
a.c:12:2: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
printf ("File contains %dA.
", count);
^
sh-4.2$
So clearly the variable type FILE requires stdio.h inclusion yet the function getc() requires type FILE* as an input
argument. Perhaps the standard C library implements getc() both as a function as well as macro depending on whether you
include stdio.h, since the variable type of macro input arguments are not explicitly predefined.
But this is a digression. Surely if you want to compile C++ you should write your code in C++? It’s not difficult to
replace getc() with the ifstream equivalent. For example…
# include <iostream>
# include <fstream>
using namespace std;
int main(int argc, char* argv]) {
int c;
int count;
ifstream infile ("a.cpp");
while (!infile.eof()) {
c = infile.get();
if (c == 'A') count++;
};
infile.close();
cout << "File contains " << count << "A" << endl;
return 0;
}
… does exactly the same thing as the straight C code above without having to fuss about getc().