Header being included twice, despite prevention

Hi,

I'm trying to compile a C++ project imported from Dev-C++ to Netbeans. I'm using MinGW under Win XP x64. I didn't like Netbeans' makefile so I wrote my own.

But when I run make (either through Netbeans or the command line), I get this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Running "I:\msys\1.0\bin\make.exe  -f Makefile CONF=Debug" in D:\Psykurve2

g++ -I"I:/MinGW/include" -I"D:/My Documents/c/src" -DGLFW -O3 -ffast-math -mconsole -Wall -fexceptions -I"I:/MinGW/include" -I"D:/My Documents/c/src" -c src/auxiliar/angles.cpp -o angles.o
g++ -I"I:/MinGW/include" -I"D:/My Documents/c/src" -DGLFW -O3 -ffast-math -mconsole -Wall -fexceptions -I"I:/MinGW/include" -I"D:/My Documents/c/src" -c src/coordlist.cpp -o coordlist.o
g++ -I"I:/MinGW/include" -I"D:/My Documents/c/src" -DGLFW -O3 -ffast-math -mconsole -Wall -fexceptions -I"I:/MinGW/include" -I"D:/My Documents/c/src" -c src/debug.cpp -o debug.o
In file included from src/debug.h:2,

                 from src/debug.cpp:1:

D:/My Documents/c/src/log/log.h:486: error: redefinition of `struct _LOG'

D:/My Documents/c/src/log/log.h:483: error: previous definition of `struct _LOG'

D:/My Documents/c/src/log/log.h:498: error: ISO C++ forbids declaration of `LOG' with no type

... a bunch of similar errors ...

make: *** [debug.o] Error 1

Build failed. Exit value 2. 


In other words, the header "log.h" is being included twice (once for angles.cpp and another one for debug.cpp). How can I prevent this from happening?

I already tried:

1
2
3
4
5
6
#ifndef __LOG__
#define __LOG__

/* LOG code */

#endif 


and:

1
2
3
#pragma once

/* LOG code */


This is my makefile: http://pastebin.com/m1f490ab9 . It's only my second makefile, so I would appreciate any advice on this issue.
In other words, the header "log.h" is being included twice (once for angles.cpp and another one for debug.cpp). How can I prevent this from happening?


log.h will be #included in whatever files you put #include "log.h" in. If you #include in both angles.cpp and debug.cpp, then it will be included in both.

However this is not a problem. It's actually kind of the whole point of using .h files.


Anyway... your problem is not what you think it is. According to the errors, I'm deducing that _LOG is defined twice in log.h, and is defined differently each time. See how the errors point to different lines:

D:/My Documents/c/src/log/log.h:486: error: redefinition of `struct _LOG'

D:/My Documents/c/src/log/log.h:483: error: previous definition of `struct _LOG'


It's erroring on line 486, saying that _LOG was already defined on line 483. So probably one of those lines is the problem.

If you could paste all lines between 480 and 490 or so of log.h, I'd be able to help further.
Thank you for your help. Log.h is correct, I know that for sure.

However, I was mistaken about make's behaviour. I tried:

make debug.o

and got the same result, so I thought there must be something wrong with the debug stuff. I checked, and I found that I was #including log.h three times! (from different headers).

debug.h:
1
2
#include "coordlist.h"
#include <log/log.h> 


coordlist.h
1
2
#include "auxiliar/angles.h"
#include <log/log.h> 


angles.h
1
2
#include <cmath>
#include <log/log.h> 


I fixed it by deleting the redundant #includes in debug.h and coordlist.h. But that doesn't seem like an elegant solution to me. #pragma once is supposed to prevent this stuff from happening, isn't it?
I fixed it by deleting the redundant #includes in debug.h and coordlist.h.


If the header is include guarded, this wouldn't have made a difference. IE:

1
2
3
4
5
6
#ifndef __LOG_H__
#define __LOG_H__

/* ... */

#endif 


If the header has that, you can #include it a million times over and it won't matter, only the first will be used. I have no idea what #pragma once does -- I avoid #pragma like the plague.

This sounds mighty fishy to me.... but if it's working now I guess that's a good thing.
#pragma once works exactly like include guards. The file has both #pragma once and the include guard code now. Doing this:

1
2
#include <log/log.h>
#include <log/log.h> 


works fine. But not when I do it across multiple headers that depend on each other. ):

I guess I should just avoid that kind of dependency entanglements. Thanks

Wikipedia reference: http://en.wikipedia.org/wiki/Pragma_once
Topic archived. No new replies allowed.