Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error WinSock.h has already been included Boost Windows Qt

Tags:

c++

boost

qt

I am using boost lib on Qt creator to develop an application, my platform is Windows8 MSVC2013 64.

I have two class on each I am using boost headers,

Now I need to use this class and when I include these class header from another source file I am getting the error

error WinSock.h has already been included

In my fist class I added boost header like

#define WIN32_LEAN_AND_MEAN
#include <boost/asio.hpp>
#include <boost/asio/deadline_timer.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/read_until.hpp>
#include <boost/bind.hpp>
#include <boost/asio/streambuf.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/foreach.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/thread_time.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <boost/bind.hpp>

#include <boost/cstdint.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/format.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>

and in second class I used the header on same way

#define WIN32_LEAN_AND_MEAN
#include <iostream>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/thread_time.hpp>
#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <boost/asio/deadline_timer.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/read_until.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/asio/streambuf.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/read.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <boost/asio/placeholders.hpp>
#include <fstream>
#include <boost/cstdint.hpp>
#include <boost/circular_buffer.hpp>
#include <boost/format.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>

I already found the answer here Boost::asio winsock and winsock 2 compatibility issue but still I am having the issue.

Edit: My second class internally using the first class header, does that be the cause like multiple time including the boost headers?

I have surrounded the boost header includes with

#ifndef class_obj__1
#define class_obj__1
//boost headers
//boost headers  
#endif

Any help will be appreciated.

Thanks Haris

like image 254
Haris Avatar asked Jun 09 '15 11:06

Haris


1 Answers

Move the #define WIN32_LEAN_AND_MEAN to g++ command line argument list as -DWIN32_LEAN_AND_MEAN. This helped me.
One more thing helped me is reordering #include directives to put boost includes before all others.

like image 172
OlegG Avatar answered Oct 19 '22 23:10

OlegG