0

To my understanding Visual Studio C++ projects use #pragma once in the very top of header files to prevent multiple inclusion.

And the same is achieved, in Qt Creator projects, with

#ifndef   FILE_H
#define   FILE_H
// Code here
#endif

Let's say I got a Visual Studio project and I want to play with Qt Creator. How to deal with the #pragma once issue?

If possible I don't want to edit all header files.

KcFnMi
  • 3,922
  • 8
  • 43
  • 90
  • 1
    I'm pretty sure `#pragma once` is supported by Qt Creator. – NathanOliver Jan 27 '21 at 16:26
  • 1
    `#pragma once` (or header guards) are processed (at least primarily) by the compiler, not the IDE. – Jerry Coffin Jan 27 '21 at 16:36
  • 2
    What problem does `#pragma once` cause? please explain – Mukul Kumar Jan 27 '21 at 16:37
  • 3
    Depending on your compiler or C++ version, `#pragma once` can be ignored, or the compiler can complain that the `#pragma` is not supported. – Thomas Matthews Jan 27 '21 at 16:42
  • 1
    And even if `#pragma once` is supported it can be fooled by a sufficiently complicated directory structure. Here's a good discussion: [#pragma once vs include guards?](https://stackoverflow.com/questions/1143936/pragma-once-vs-include-guards) Don't stop at the first answer. There are a number of conflicting views and to really get the issues, you have to read down several answers. – user4581301 Jan 27 '21 at 17:00
  • Never heard about a real world problem with pragma once the last >10 years. And if still a preprocessor ist not able to see that ../bla/xy.h is the same as ../../blub/bla/xy.h... Not really in 2021 I believe :-) There are some really obscure but also outdated compilers, which had that problem... ok, but long time ago! And as you already have pragma once in your code, simply leave it as it is as long it did not make trouble, If you need to change, do it if the problem really occurs, but not before! – Klaus Jan 27 '21 at 18:28
  • @Klaus I admit you'd really have to you'd have to work at it. It's about as likely as someone cut-n-pasting or otherwise using the same include guard identifier. – user4581301 Jan 27 '21 at 23:42
  • +1 never seen issues with pragma once so far, and using it in few large projects on Linux with gcc. If ever problem occurs, only then it is worh to add standars include guard, otherwise worth to use pragma once by default. – ivan.ukr Jan 30 '21 at 09:28
  • It almost seems `#pragma once` is better than the alternative. – KcFnMi Feb 01 '21 at 15:33

1 Answers1

1

Yes, really, #pragma once originates from Microsoft C++, but nowadays it is supported by two another most used modern C++ compilers (i.e GCC and clang). So you should not have a problems with it.

ivan.ukr
  • 2,111
  • 14
  • 33