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?
-
5There is absolutely no logic behind the preprocessors handling of `#include`. It is really just a copy/past, that's it. – dtell Feb 10 '21 at 11:16
-
@mkrieger1 i know how to include a file that is in a directory, you just have to specify the path. What i wanna know is how to automate the process – Giuppox Feb 10 '21 at 11:17
-
Similarly, [How to include all source files from a folder?](https://stackoverflow.com/questions/3750831/how-to-include-all-source-files-from-a-folder) – mkrieger1 Feb 10 '21 at 11:20
-
Have a look at CMake, it is a quite common way to manage the project. – Adrian Maire Feb 10 '21 at 11:21
-
@mkrieger1 i have already done this thing. My project has a file in it where the whole code of the library is included. However i want to include that file (from an extern one) just by doing `#include "project"` (or something similar) and not `#include "project/init.h"` – Giuppox Feb 10 '21 at 11:23
-
There is no such thing in C++11: You can have project management helping (e.g. Cmake), you can load/unload dynamic libraries runtime to add/remove "modules". Beside this, you have the #include which basically is a dummy copy/paste. Since C++20 you have "Modules" but they don't work as Python ones. – Adrian Maire Feb 10 '21 at 11:25
-
In C/C++ you really wat to include the **necesary** for each file, I think it's a better practice to have several (but really necesary) `#include` lines than a general (that may unclude unnecessary files) one – Ivan Feb 10 '21 at 11:30
-
Starting with C++20 you can create your own modules in C++. For C++11 there's no such feature. – Elijan9 Feb 10 '21 at 11:36
2 Answers
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.

- 34,786
- 15
- 102
- 130
-
Hi thanks for you answer. What is the method used by, let's say, `#include
`? That adds several functions to `std`. – Giuppox Feb 10 '21 at 11:43 -
1@Giuppox These functions are either defined in this single include file (`any.h`) or in headers transitively included by `any.h`. – BartoszKP Feb 10 '21 at 11:49
-
1@Giuppox it looks through the system include paths (not recursively) for a file named `any`, and then copy-pastes the content of the first one it finds. On my system it's `/usr/include/c++/7/any` – OrangeDog Feb 10 '21 at 11:52
-
3Standard hearders don't have an extention, so `#include
` looks for a file named `any`, not `any.h` or `any.hpp`. – IlCapitano Feb 10 '21 at 11:53
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.

- 2,357
- 1
- 17
- 27
-
FWIW, it doesn't behave the same, but this is probably the closest thing to defining an `__all__` array in `__init__.py`. – Elijan9 Feb 10 '21 at 12:35
-
Yep, I do try to highlight that fact. Also, you have to write the header manually, which is probably the pain-point from a python-dev's perspective. – Mansoor Feb 10 '21 at 12:37