In Python when you create a module you create an __init__.py
file (at least, usually). Is there a C/C++ equivalent of this thing? I mean is there a way to #include
a directory actually including a file inside it?
The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.
If you have setup.py in your project and you use find_packages() within it, it is necessary to have an __init__.py file in every directory for packages to be automatically found.
The __init__.py file makes Python treat directories containing it as modules. Furthermore, this is the first file to be loaded in a module, so you can use it to execute code that you want to run each time a module is loaded, or specify the submodules to be exported.
Leaving an __init__.py file empty is considered normal and even a good practice, if the package's modules and sub-packages do not need to share any code.
No, there is no such feature in C++ itself. Nor it seems the typically used compilers support it. A feature similar to Python's modules is introduced in C++20: https://en.cppreference.com/w/cpp/language/modules
You may want to look at build systems like SCons or CMake which will allow you to implement some preprocessing before the actual C++ preprocessing/compilation. For example, you can use them to generate a header file including all the headers from a directory, or do anything more complicated if you really need it.
Please do take into consideration the last part of the last sentence: do you really need it? Usually code is much easier to maintain if all its dependencies are explicit. Having a header including "everything" will make it hard to track. One can imagine some valid reasons for breaking this rule of course, e.g. if these headers are generated as well and it's desirable to have an automated way of including all of them. Still, it's best if scope of such "magic" is self-contained and as small as possible.
Is there a C/C++ equivalent of this thing?
Not equivalent, but for header-only libraries, it is common practise to include an aggregate header, which is a header than includes other header files. For example, if we look at boost filesystem:
/mnt/e/Repository/filesystem/include/boost/
├── filesystem
│ ├── config.hpp
│ ├── convenience.hpp
│ ├── detail/
│ ├── directory.hpp
│ ├── exception.hpp
│ ├── file_status.hpp
│ ├── fstream.hpp
│ ├── operations.hpp
│ ├── path.hpp
│ ├── path_traits.hpp
│ └── string_file.hpp
└── filesystem.hpp **Aggregate header**
Contents of filesystem.hpp:
...
# include <boost/filesystem/config.hpp>
# include <boost/filesystem/path.hpp>
# include <boost/filesystem/exception.hpp>
# include <boost/filesystem/directory.hpp>
# include <boost/filesystem/operations.hpp>
# include <boost/filesystem/file_status.hpp>
# include <boost/filesystem/convenience.hpp>
# include <boost/filesystem/string_file.hpp>
...
Note that this does not behave the same as the __init__.py
file, it is a convenience feature. You need only include the aggregate header, to access all of the functionality.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With