Reputation: 12334
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:
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
Upvotes: 3
Views: 11527
Reputation: 30035
I seem to remember having this problem. I think there is a dependency issue between windows.h and winsock2.h I seem to remember I got around it by always including windows.h before winsock2.h wherever it was used.
Upvotes: 0
Reputation: 63
I had the same problem recently and solved it including winsock2.h
before including windows.h
.
Upvotes: 2
Reputation: 6295
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.
Upvotes: 1
Reputation: 57202
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.
Upvotes: 7
Reputation: 11808
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!
Upvotes: 0
Reputation: 4689
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 */
Upvotes: 1