C++ redefinition due to including header files multiple times

akif picture akif · Sep 3, 2009 · Viewed 9.9k times · Source

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

Answer

Paolo Tedesco picture Paolo Tedesco · Sep 3, 2009

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.