r/C_Programming • • 27d ago

Could someone explain the practical differences between #pragma once and traditional include guards (#ifndef, #define, and #endif)?

84 Upvotes

58 comments sorted by

View all comments

Show parent comment

-4

u/Huuf 26d ago

I don't fully agree with the including the same header twice is bad part, for example, a string library, if you can only use a string library handling in one spot, you won't get very far. Or an animal header, where the dog.c and cat.c both include it. Also, by definition, one header will always be included twice, since the implementation includes it, and the caller needs to include it.

7

u/HugoNikanor 26d ago

You're misunderstanding him. It's perfectly sensible for both doc.c and cat.c to #include "string-lib.h". The issue OP is talking about is if you do something like

$ ln string-lib.h other-name.h

Then in file.c you write:

#include "string-lib.h"
#include "other-name.h"

With include guards, the compiler doesn't care that you used two different names for one file, since the include guards are the same. However, with #pragma once the compiler could see that as two different files, and actually include the contents of both

1

u/Huuf 26d ago

If this is meant, I totally agree

3

u/Emotional-Audience85 26d ago

I don't think he said anything of the sort

0

u/Huuf 26d ago

I interpreted this text as such:

in my opinion, if you have this problem where you are somehow getting the same header included through different paths there is something wrong with your file layout in most all cases (others are free to disagree).

3

u/Emotional-Audience85 26d ago

I interpreted it has having the file included through different physical paths (e.g. one is a symlink to the same file).

I've never encountered this but I believe it may be an issue for #pragma, as the same file may be treated as 2 different files and the content be pasted twice, leading to compilation errors

1

u/Huuf 26d ago

If that is meant, I totally agree,

3

u/FedUp233 26d ago

Perhaps I was not as clear as I should have been. As others mentioned, it the issue of including the same header more than once in the same C or C++ file through different symlink aliases that is the issue. Like I said, I’ve never actually seen this happen but it theoretically could if the same header was referenced through different paths in different header files that included it and both this header files were included in the same C or C++ file (the same compilation unit is I think what the standard uses) either directly or indirect.

Of course the same header can and needs to be included in different compilation units. And it’s fine if different compilation units use different paths to include a header file as long as the path to a given header is consistent within each compilation unit.

2

u/Huuf 26d ago

Yeah, sorry about that.

2

u/not_a_novel_account 26d ago

Like I said, I’ve never actually seen this happen but it theoretically could

This becomes more and more common the larger the organization and codebase is.

It can become pathological when build servers are accessing files through network file mounts.

This is the exact kind of problem which killed the standardization effort.

1

u/FedUp233 26d ago

As I said, it’s obviously theoretically possible, though I’m not sure why network mounts would make the problem any worse as far as having multiple paths to the same include file from within one compilation unit.

I can see however how a standardization body, which tries to get every corner case specified, would have problems agreeing on a perfect definition. I would think they might have just taken the UB route they took in many other places and said that in a case where the same file is included multiple times through multiple different paths, it’s UB. And just defined it for normal, not pathological cases.

I can certainly see an include having different paths in different compilation units, but that’s fine. If I was dealing with a code base that exhibited this problem, I think my first thought would be to fix it, but I’ve worked for a Marge company and I can see how management might just push it down the road and hope it never gets worse.

2

u/not_a_novel_account 26d ago

why network mounts would make the problem any worse as far as having multiple paths to the same include file from within one compilation unit.

Because they overlap. Team Tiger in Shanghai uses #include <widget.h> and Team Dragon in Mumbai uses #include <component/widget.h>, both Dragon's and Tiger's headers are used by Team Eagle in Chicago. Each team builds, tests, and verifies their own code.

The entire thing works because the build servers mount subcomponents into /opt/BigCorp/<whatever> and set include flags to various places there. Different teams require subcomponents mounted in different places, and so downstream projects can have the same subcomponent mounted multiple times at different locations.

When switching to #pragma, suddenly widget.h is ambiguous. It's being accessed via different paths for the Team Tiger and Team Dragon code, and now Team Eagle is seeing definition collisions that they have no insight into.

This is pretty much the exact arrangement that caused #pragma to die during standardization. It's not a hypothetical. Major WG14 participants reported back on their experience with #pragma and required this be solved. It couldn't be, so standardization died.

1

u/FedUp233 26d ago

That makes sense. But I think I’d put this in the poor team leadership/management bucket. Someone in charge of the whole project should set standards for things like this and tell the teams “this is how it will be done” after getting input from both teams on why they chose a particular method. It the job of management and technical leads to make sure this type of stuff doesn’t happen!