r/C_Programming • • 25d ago

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

81 Upvotes

58 comments sorted by

View all comments

Show parent comment

2

u/not_a_novel_account 25d 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 24d 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 24d 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 24d 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!