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?

6
  • the thing inside has nothing to do with the language. Its the name of the file you are including. #include <stdio.h> is as valid C as it is C++
    – Creris
    Apr 18, 2015 at 20:41
  • @Creris, Maybe not equally valid, though, since stdio.h is deprecated in C++.
    – chris
    Apr 18, 2015 at 20:43
  • 1
    For the standard headers, prefer the <cstdio> alternatives to <stdio.h>. For any other library, you can't just simply decide which to use: you have to match the file name.
    – stefan
    Apr 18, 2015 at 20:44
  • @chris Deprecated perhaps, but I haven't found an implementation without it. Apr 18, 2015 at 20:44
  • @dr: That's because it's deprecated, not removed. (also, language extensions)
    – user1084944
    Apr 18, 2015 at 20:46

2 Answers 2

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.

4
  • 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.
    – user1795160
    Apr 18, 2015 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, 2015 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. Apr 18, 2015 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, 2015 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.

1
  • I mean standard headers. You misunderstood me. I will fix it.
    – gomons
    Apr 18, 2015 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.