views:

851

answers:

4

As, the title says. I'm encountering redefinition errors due to including header files multiple times. I know its because of that, but I don't know how to resolve. Yes, I previously posted the same problem in SO an hour ahead. But I wasn't able to explain properly (I think so) and didn't get answers expected. Here is the link:

C++ Redefinition Header Files

I'm not editing that question since it has been filled up :).

Okay I have some classes and the structure of them is like this:

main.cpp:

#include "Server.h"
#include "Handler.h"
#include "Processor.h"

int main(int argc, char* argv[])
{

}

Server.h:

// Server.h
#pragma once

#include <winsock2.h>

Handler.h:

// Handler.h
#pragma once

#include <string>
#include <vector>

#include "Server.h"

Processor.cpp:

// Processor.cpp

#include "StdAfx.h"
#include "Processor.h"
#include "Handler.h"

Server.cpp:

// Server.cpp

#include "Server.h"
#include "Processor.h"

The problem is that <winsock2.h> is included multiple times, don't know where but it is. #pragma once serves the same purpose as

#ifndef SOME_FILE_H
#define SOME_FILE_H
// code here
#endif // SOME_FILE_H

in my compiler (MSVC2008 in this case). So I'm pretty much sure I don't need the header include guards. But can you spot where I'm doing the mistake by which <winsock2.> is included twice and how may I resolve?

Thanks

A: 

You need some or all of these before you include stdafx or windows.

#define _MSWSOCK_
#define NCB_INCLUDED
#define _WINSOCK2API_
#define _WINSOCKAPI_   /* Prevent inclusion of winsock.h in windows.h */
Corey D
Can you explain why?
hab
@Manzoor: it's the way the MS headers are laid out. You have to follow the rules, but I don't know where the rules are documented. When working with the MS toolchain, I've always found it advantageous to just have their wizard generate the initial empty project. That insures that everything is setup correctly, and keeps you from spending hours on this kind of thing.
Michael Kohne
I have no idea why. I had this problem in my project because I had to include an outside header which uses MFC. While it's likely possible to get around this, this solution was quick and it works with no side effects.
Corey D
A: 

Have you tried any of the suggestions we made in your other answer?

Seriously, try using include guards instead of #pragma once.

If you still get the problem, then maybe come back and post another question on SO. Don't post multiple questions about the same thing because you're unwilling (or unable) to accept our advice!

Thomi
Apart from its MS-specificity, what's wrong with #pragma once?
Paolo Tedesco
Have you not read my response in the other question?
hab
Seriously, your suggestion is "I don't use either MSVC or GCC, so you shouldn't use non-standard features of those compilers"? If someone has a problem with threads, we don't say, "first make your program single-threaded, otherwise you can't expect anyone to help you".
Steve Jessop
Where was this "suggestion" made? All I'm saying is that we do generally ask people to not post duplicate, or near-duplicate questions without reading the answers people gave in the first place!
Thomi
Oh, sorry, that's fair enough. I thought you were saying that you can't answer the question until #pragma once has been removed. But I think the questioner has read those answers, and indicates as much in this question, and has posted the followup question, "assuming that #pragma once does actually work, same thing as before". So he's read those answers, just doesn't think they have anything to do with the question he wanted to ask in the first place.
Steve Jessop
+3  A: 

In your project settings: Project properties -> configuration -> advanced -> show includes.

It will dump the header include tree, from there you'll be able to see the culprit.

Paolo Tedesco
there's a useful tip!
xtofl
A: 

try replacing

#include <winsock2.h>

with

#ifndef _WINSOCK2API_
#include <winsock2.h>
#endif

Since _WINSOCK2API_ is defined inside winsock2.h, compiler will not try to include it multiple times.

erelender
Tried, no luck :(
hab