Check if #pragma once is supported at compile time?

This is a discussion on Check if #pragma once is supported at compile time? within the C++ Programming forums, part of the General Programming Boards category; Hi, is there any way to check if Code: #pragma once is supported by the compiler, during compile time? If ...

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Check if #pragma once is supported at compile time?

    Hi, is there any way to check if

    Code:
    #pragma once
    is supported by the compiler, during compile time?

    If it is supported I want to call it since it may speed up the compilation, but if it's not supported the line will generate a warning which I don't want to have.
    Come on, you can do it! b( ~_')

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    If you want to be portable, just use conventional header inclusion guards. Placing them at the very start of the header, before any introductory comments, would generally suffice for quick detection that a header has already been included.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Yes, I know, I use ordinary inclusion guards too but I still want to use pragma once if it is supported. They do so here in the boost bind library. However, they only use pragma once when the compiler is the Visual C++ compiler, but almost all large compilers supports this pragma directive and hence they miss out a lot of them.
    Come on, you can do it! b( ~_')

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Quote Originally Posted by TriKri
    However, they only use pragma once when the compiler is the Visual C++ compiler, but almost all large compilers supports this pragma directive and hence they miss out a lot of them.
    That may be because MSVC does not (did not?) support intelligent detection of header inclusion guards, so #pragma once would have been an optimisation for compiling. Furthermore, historically, even those compilers that did support #pragma once may have bugs in that support, e.g., because a header file may be included by different paths.

    Anyway, no: there is no way to check from within the code if #pragma once is supported (correctly). You have to either live with the warning, silence it, or list the compilers that you want to support and expand that list you saw in the Boost Bind header.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Okay, thanks for your answer. So compilation may not go that much faster with pragma once because many compilers have intelligent detection of header inclusion guards? That kind of makes sense, since the compiler I am using (qmake, Qt's copiler) supports pragma once but never uses it. Instead, it automatically wraps all newly created header files in ordinary inclusion guards. On the other hand, in that case it is strange that Boost bind still uses both. Ah well.
    Come on, you can do it! b( ~_')

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile time
    By onako in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2011, 03:43 AM
  2. Getting compile time.
    By ThatDudeMan in forum C Programming
    Replies: 2
    Last Post: 10-13-2010, 12:15 PM
  3. Replies: 8
    Last Post: 06-03-2009, 03:00 PM
  4. Compile-Time Help
    By brayden37 in forum C++ Programming
    Replies: 0
    Last Post: 10-11-2001, 03:10 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21