2

#pragma once is not working properly when compiling a project I have been working on. Having read up on the topic a bit, I saw that the one downside to using it is that if the same file is in different locations then it will not work like the other way of using include guards like so:

#ifndef __SOMETHING_H__ 
#define __SOMETHING_H__ 

Here is the error:

../../engine/scene/../common/../bitsquid/queue.h:78:29: error: redefinition of    ‘template<class T> void foundation::queue::reserve(foundation::Queue<T>&, uint32_t)’
../../engine/scene/../common/../bitsquid/queue.h:78:29: error: ‘template<class T> void foundation::queue::reserve(foundation::Queue<T>&, uint32_t)’ previously declared here

You can see that the path is the same. I am using g++ to compile the source. I would usually just replace this with the other style of include guards but I would like to avoid that as I did not write the library.

gcc version is 4.6.3

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
Jeff Melton
  • 357
  • 4
  • 13

1 Answers1

7

the definition of #pragma once is that the current file is only included once.

If you copy it somewhere else, then the file is different, which means that it's considered different from the #pragma once perspective.

If you want to copy the file somewhere else and get a similar behaviour to #pragma once, then you need to use include guards.

also you shouldn't be using __HEADER_H__ for the include guard, the use of a leading __ is reserved and can lead to subtle bugs.

Additionally, having looked at the code on bitbucket, the file queue.h has neither an #include guard, nor has it a #pragma once guard, which indicates a false premise in this case.

If you're trying to munge a bunch of .cpp files into a single one, and they are #includeing the same file in this case then you'll get this exact error.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122