You are not logged in.

#1 2009-07-15 21:04:05

Basu
Member
From: Cornell University
Registered: 2006-12-15
Posts: 296
Website

.h extension on C headers

I just started writing C and run into a problem which I've run into before but never really found an answer to:
I've seen standard headers included in 2 forms:
<stdio> or <stdio.h>

Running GCC with no options fails to find the file if there's no .h. But I previously wrote a medium-ish project in C++ using Qmake to generate Makefiles and it worked fine without the .h.

What is going on here? Does leaving out the extension require some libraries that GCC doesn't use by default but the generated Makefile includes?


The Bytebaker -- Computer science is not a science and it's not about computers
Check out my open source software at Github

Offline

#2 2009-07-15 21:39:56

bremac
Member
Registered: 2008-09-13
Posts: 13

Re: .h extension on C headers

I've never heard of including <stdio>; perhaps you're misremembering <cstdio>-style (the proper way to include system headers in C++, ie. with g++) vs <stdio.h>-style (C/gcc)? In C++ system headers tend to lack an extension (ie. #include <cstring>, #include <vector>, etc.), however the C standard headers are named with a 'c' prefix as well as lacking the '.h' suffix under complaint C++ compilers. I truly doubt you've ever seen an included header precisely named as '<stdio>'.

Last edited by bremac (2009-07-15 21:50:29)

Offline

#3 2009-07-15 21:42:24

quetzyg
Member
From: /home/quetzyg
Registered: 2006-08-03
Posts: 129

Re: .h extension on C headers

Hi Basu!

In C you have to put the file extension on the #include directive, but in C++ you can omit it, since there isn't a standard for those files.
You could have iostream, iostream.h or iostream.hpp.

Google around, and you'll find better info wink

Cheers,
Quetzy


ZzZz...

Offline

#4 2009-07-15 21:49:01

bremac
Member
Registered: 2008-09-13
Posts: 13

Re: .h extension on C headers

quetzyg wrote:

In C you have to put the file extension on the #include directive, but in C++ you can omit it, since there isn't a standard for those files.
You could have iostream, iostream.h or iostream.hpp.

Actually, that's not quite correct. The standard C++ headers lack extensions on the filesystem instead of simply allowing them to be elided, and must be correctly named as such - thus including 'iostream.hpp' or 'iostream.h' will not work.

Offline

#5 2009-07-15 23:16:01

Basu
Member
From: Cornell University
Registered: 2006-12-15
Posts: 296
Website

Re: .h extension on C headers

Hi bremac, you're probably right. I didn't think that C and C++ would be different in that regard, but now that you mention it, it does make sense. People tend to use C and C++ together so much that the differences can often be forgotten. Thanks for clearing that up for me.


The Bytebaker -- Computer science is not a science and it's not about computers
Check out my open source software at Github

Offline

Board footer

Powered by FluxBB