1

I have just learned C++ -- and I decided one project I am working for, I am better off going back to the drawing board and writing it from scratch in C++, rather than trudging on with C.

There is just one concern --- part of this project includes libraries. Some libraries will not be needed in C++, but some will.

I notice that the syntax for including library headers is different in C++ than it is in C. In C you write the following:

#include <someheader.h>

On the other hand, in C++ what you type is the following (if it is a C++ library):

#include <someheader>

Because of this, I am wary that there might be some differences in how I put together a C++ header file than in how I put together a C header file -- or at least some difference in how I name it in the file-system.

So does anyone have any information what I need to know in putting together a C++ library-header file as opposed to one for C?

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Sophia_ES
  • 1,193
  • 3
  • 12
  • 22
  • the thing inside has nothing to do with the language. Its the name of the file you are including. `#include ` is as valid C as it is C++ – Creris Apr 18 '15 at 20:41
  • @Creris, Maybe not equally valid, though, since `stdio.h` is deprecated in C++. – chris Apr 18 '15 at 20:43
  • 1
    For the standard headers, prefer the `` alternatives to ``. For any other library, you can't just simply decide which to use: you have to match the file name. – stefan Apr 18 '15 at 20:44
  • @chris Deprecated perhaps, but I haven't found an implementation without it. – The name's Bob. MS Bob. Apr 18 '15 at 20:44
  • @dr: That's because it's _deprecated_, not _removed_. (also, language extensions) –  Apr 18 '15 at 20:46
  • @chris depcrecated or not, still valid C++ so my point stands – Creris Apr 18 '15 at 20:49

2 Answers2

5

There is no difference. Most, if not all, of the standard C++ library include files do not have a .h extension, to distinguish them from C library includes. The original C standard header file names are deprecated in C++, although virtually every compiler still supports them, and changed in name to c followed by the original C file name, without the .h extension.

For example: In C, the header file relating to strings is string.h, but the C++ header file relating to strings is string. The original C header file can also be accessed in C++ as cstring.

  • It is also worth mentioning that the header `string.h` is deprecated in C++. You should use `cstring` instead (same with most, if not all standard C headers). This also solves the 'ambiguity' between `string` and `string.h` since `cstring` clearly states that you are including a standard C header. –  Apr 18 '15 at 20:55
  • In other words, if I understand, you are saying that I just need to name the file "someheader" rather than "someheader.h" and that is the only difference. Thank you very much --- I just wanted to be *sure* it was this simple, rather than be greeted by a surprise. – Sophia_ES Apr 18 '15 at 20:57
  • 2
    @Sophia_ES NO! Not what I'm saying. You should still name your own header files with the `.h` extension. It's only the C++ standard library names that don't have an extension. – The name's Bob. MS Bob. Apr 18 '15 at 20:59
  • 1
    A practical case, when having the `.h` extension matters: XCode IDE does not highlight the code in a file with no extension (steps to reproduce: create a project, create a header file, remove extension, add some code, save, reload the project, look at the code). This doesn't apply to the standard library headers, they are highlighted well. – Vladimir Apr 18 '15 at 21:20
0

Feel free to mix standard C headers with standard C++ headers. But be consistent. For C++ project use <cstdlib>, for C use <stdlib.h>, because C++ headers uses namespaces and you can avoid name collisions. All standard C headers duplicated in C++, so you can freely use C library in C++ code.

If you look to <cstdlib> you will see that it includes <stdlib.h>. It is true for other standard C headers.

gomons
  • 1,946
  • 14
  • 25