What is the best practice for a shared library core header file in C ++?

When I create shared libraries, I have a header file (but without a file name extension) in the root directory of the library source, just like the library.

So, for example, if my library was called libirock.so, then I would have a file called irock in the root of the project. This file will contain all the most important headers in the library, so when the library is implemented, all you have to do is use this include line:

#include <irock> // Instead of <irock.h> 

I had an idea when I saw a compiler warning like this:

 #include <string.h> is obsolete, use #include <string> instead 

Two questions:

  • Uses irock instead of irock.h best practice?
  • Is it right to use one header file instead of many headers?

Course of action

Thank you for your responses! From the answers I decided:

  • Will use <irock.h> instead of <irock> .
  • I will continue to use the header file 'primary'.
+4
source share
5 answers

In the standard resolution, “allowed,” “forbidden,” or “best practices” regarding file name extensions are nothing.

Use the shape you prefer. On some platforms, there is a convenience factor in the presence of file extensions for registered types.

Why is <string.h> and <string> completely different headers. The named equivalent of <string.h> in C ++ is actually <cstring> .

+4
source

In one word, no. Do you want to explicitly use irock.h

With the in-place extension, it is clear that this is a header file, and any application that uses files based on the file extension knows correctly how to interpret your header file.

+6
source

No, <header> instead of <header.h> It is assumed that the idiom is intended only for standard libraries (and the standard template library).

+4
source

#include simply puts the contents of a specific file name in the actual file. So if you feel better without the file extension, just do it.

Of course, the file extension has semantic meaning, which is useful. In addition, included files without an extension are associated with the standard library in most user minds.

0
source

What is used in Qt4 is that you can include file names by class name, for example

 #include <QString> #include <QWidget> #include <QPainter> #include <QApplication> #include <QCoreApplication> 

These include dummy elements that will contain the correct title. In real life, you will see that several classes are defined in one include.h, and you have included this file twice.

The reason for using a version without ".h" is that you will be tempted to use include, which are camelCase (or PascalNotation), and if you move code from windows to unix (linux or mac), you will usually get into problems . Not difficult to do, but you need to take care to do it right.

0
source

All Articles