Atop explanation by Konrad Kleine above.
A brief summary:
- when we use
# pragma once
it is much of the compiler responsibility, not to allow its inclusion more than once. Which means, after you mention the code-snippet in the file, it is no more your responsibility.
Now, compiler looks, for this code-snippet at the beginning of the file, and skips it from being included (if already included once). This definitely will reduce the compilation-time (on an average and in huge-system). However, in case of mocks/test environment, will make the test-cases implementation difficult, due to circular etc dependencies.
- Now, when we use the
#ifndef XYZ_H
for the headers, it is more of the developers responsibility to maintain the dependency of headers. Which means, whenever due to some new header file, there is possibility of the circular dependency, compiler will just flag some "undefined ..
" error messages at compile time, and it is user to check the logical connection/flow of the entities and rectify the improper includes.
This definitely will add to the compilation time (as needs to rectified and re-run). Also, as it works on the basis of including the file, based on the "XYZ_H" defined-state, and still complains, if not able to get all the definitions.
Therefore, to avoid situations like this, we should use, as;
#pragma once
#ifndef XYZ_H
#define XYZ_H
...
#endif
i.e. the combination of both.