NHIỀU BẠN THẮC MẮC #PRAGMA ONCE LÀ GÌ

  -  
I"m working on a codebase that is known to only run on windows & be compiled under Visual Studio (it integrates tightly with excel so it"s not going anywhere). I"m wondering if I should go with the traditional include guards or use #pragma once for our code. I would think letting the compiler khuyễn mãi giảm giá with #pragma once will yield faster compiles & is less error prone when copying & pasting. It is also slightly less ugly ;)

Note: khổng lồ get the faster compile times we could use Redundant Include Guards but that adds a tight coupling between the included tệp tin & the including tệp tin. Usually it"s ok because the guard should be based on the tệp tin name & would only change if you needed to change in the include name anyways.

Bạn đang xem: Nhiều bạn thắc mắc #pragma once là gì

Quý khách hàng đã xem: C — thực hiện #pragma vào c

c++ coding-style Share Improve this question Follow edited Nov 22 "18 at 3:07
*

Jonathan Leffler 666k126126 gold badges810810 silver badges11851185 bronze badges asked Jul 17 "09 at 15:18
*

Matt PriceMatt Price 38.7k99 gold badges3434 silver badges4343 bronze badges 0 Add a phản hồi |

13 Answers 13

Active Oldest Votes354I don"t think it will make a significant difference in compile time but #pragma once is very well supported across compilers but not actually part of the standard. The preprocessor may be a little faster with it as it is more simple khổng lồ understvà your exact intent.

#pragma once is less prone khổng lồ making mistakes and it is less code to lớn type.

To tốc độ up compile time more just forward declare instead of including in .h files when you can.

I prefer to use #pragma once.

Xem thêm: Lỗi Nhận Diện Thông Tin Từ Máy Chủ Fifa Online 3 Dcom, Giúp Bạn Khắc Phục

See this wikipedia article about the possibility of using both.

Share Improve sầu this answer Follow edited Jul 17 "09 at 15:34 answered Jul 17 "09 at 15:21
*

Brian R. BondyBrian R. Bondy 314k114114 gold badges576576 silver badges619619 bronze badges 19 | Show 14
more comments 182I just wanted to add khổng lồ this discussion that I am just compiling on VS and GCC, and used to use include guards. I have now switched to lớn #pragma once, và the only reason for me is not performance or portability or standard as I don"t really care what is standard as long as VS & GCC tư vấn it, & that is that:

It is all too easy to copy & paste a header file to lớn another header tệp tin, modify it to lớn suit ones needs, and forget khổng lồ change the name of the include guard. Once both are included, it takes you a while to lớn traông chồng down the error, as the error messages aren"t necessarily clear.

Share Improve this answer Follow edited Nov 30 "14 at 9:57 answered Jul 22 "11 at 16:55
*

CookieCookie 10.8k1313 gold badges4747 silver badges6969 bronze badges 2 Add a comment | 146

#pragma once has unfixable bugs. It should never be used.

If your #include search path is sufficiently complicated, the compiler may be unable lớn tell the difference between two headers with the same basename (e.g. a/foo.h & b/foo.h), so a #pragma once in one of them will suppress both. It may also be unable to lớn tell that two different relative includes (e.g. #include "foo.h" và #include "../a/foo.h" refer to lớn the same tệp tin, so #pragma once will fail to lớn suppress a redundant include when it should have.

Xem thêm: Mạng Thiên Thượng Hỏa Hợp Màu Gì, Màu Gì, Màu Và Mệnh Hợp Với Thiên

#pragma once has no such safety net -- if the compiler is wrong about the identity of a header tệp tin, either way, the program will fail khổng lồ compile. If you hit this bug, your only options are khổng lồ stop using #pragma once, or to rename one of the headers. The names of headers are part of your API contract, so renaming is probably not an option.

(The short version of why this is unfixable is that neither the Unix nor the Windows filesystem API offer any mechanism that guarantees to lớn tell you whether two absolute pathnames refer lớn the same tệp tin. If you are under the impression that inode numbers can be used for that, sorry, you"re wrong.)

(Historical note: The only reason I didn"t rip #pragma once và #import out of GCC when I had the authority to bởi so, ~12 years ago, was Apple"s system headers relying on them. In retrospect, that shouldn"t have stopped me.)

(Since this has now come up twice in the comment thread: The GCC developers did put quite a bit of effort into lớn making #pragma once as reliable as possible; see GCC bug report 11569. However, the implementation in current versions of GCC can still fail under plausible conditions, such as build farms suffering from cloông chồng skew. I vì chưng not know what any other compiler"s implementation is lượt thích, but I would not expect anyone to lớn have done better.)