Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

c++ - error WinSock.h has already been included Boost Windows 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...