# G++: a little question



## YZMSQ (May 5, 2014)

Hi,
Yesterday, I tried to compile libmf (a recommender system) on FreeBSD 10 amd64, using G++ 4.8 from ports. However, it failed with many errors like this one:

```
src/convert.cpp:16:60: error: 'printf' was not declared in this scope
     printf("usage: libmf convert text_file [binary_file]\n");
```
On the other hand, it can be compiled under Linux and OpenBSD successfully (using G++ 4.8 too). I know I can add `#include<cstdio>` to `src/convert.cpp`, to eliminate the errors, but why there are no errors on Linux and OpenBSD if cstdio is required by printf?

P.S.
And I just write a very simple C++ source file called iprintf.cpp and try to compile it on Linux, FreeBSD and OpenBSD using G++ 4.8:

```
#include<iostream>

int
main() {
	printf("printf");
	return 0;
}
```

1. Fedora Linux 20
When I compile it with the command `g++ -o iprintf iprintf.cpp`, it complains 'printf' was not declared in this scope; however, if I use `g++ -o iprintf iprintf.cpp -std=c++0x`, it compiles successfully.

2. FreeBSD 10.0
No matter I use  `g++48 -o iprintf iprintf.cpp` or  `g++48 -o iprintf iprintf.cpp  -std=c++0x`, it always fails with  'printf' was not declared in this scope.

3. OpenBSD 5.5
It compiles successfully no matter I use `eg++ -o iprintf iprintf.cpp` or `eg++ -o iprintf iprintf.cpp -std=c++0x`.

I'm now confused... :OOO


----------



## freethread (May 5, 2014)

It's 'immoral' including header files by default. If you need it, include it. Expecially standard C headers in C++ projects.


----------



## YZMSQ (May 5, 2014)

freethread said:
			
		

> It's 'immoral' including header files by default. If you need it, include it. Expecially standard C headers in C++ projects.


So you mean GCC includes some header files by default? Could I tell GCC not to include headers by default? :O


----------



## freethread (May 5, 2014)

I don't know if there are differences from GCC on Linux and FreeBSD. I compiled this library with gcc48 on FreeBSD 10.0-RELEASE, it compile if I add the two headers


```
#include <cstdlib>
#include <cstdio>
```

in mf.h

To compile it with clang you also need to


```
#include <numeric>
```

and adjust the Makefile compiling and linking flags.

On Windows... this is another story, cl is like clang, apart for POSIX specific functions.

The term 'standard' doesn't have the mean you think, not only for C and C++.


----------



## YZMSQ (May 6, 2014)

freethread said:
			
		

> I don't know if there are differences from GCC on Linux and FreeBSD. I compiled this library with gcc48 on FreeBSD 10.0-RELEASE, it compile if I add the two headers
> 
> 
> ```
> ...


Thank you, it can compile now. In fact, I just wonder why many guys don't add those simple header files. :e


----------



## freethread (May 6, 2014)

YZMSQ said:
			
		

> Thank you, it can compile now. In fact, I just wonder why many guys don't add those simple header files. :e



And why use printf instead of cout... and a namespace with no name. Whatever works.


----------

