1

I am trying to run the following code example for Boost Asio on VS2010 - 64bit. Taken from Link

#include <boost/asio.hpp> 
#include <boost/array.hpp> 
#include <iostream> 
#include <string> 

boost::asio::io_service io_service; 
boost::asio::ip::tcp::resolver resolver(io_service); 
boost::asio::ip::tcp::socket sock(io_service); 
boost::array<char, 4096> buffer; 

void read_handler(const boost::system::error_code &ec, std::size_t bytes_transferred) 
{ 
    if (!ec) 
    { 
        std::cout << std::string(buffer.data(), bytes_transferred) << std::endl; 
        sock.async_read_some(boost::asio::buffer(buffer), read_handler); 
    } 
} 

void connect_handler(const boost::system::error_code &ec) 
{ 
    if (!ec) 
    { 
        boost::asio::write(sock, boost::asio::buffer("GET / HTTP 1.1\r\nHost: highscore.de\r\n\r\n")); 
        sock.async_read_some(boost::asio::buffer(buffer), read_handler); 
    } 
} 

void resolve_handler(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator it) 
{ 
    if (!ec) 
    { 
        sock.async_connect(*it, connect_handler); 
    } 
} 

int main() 
{ 
    boost::asio::ip::tcp::resolver::query query("www.highscore.de", "80"); 
    resolver.async_resolve(query, resolve_handler); 
    io_service.run(); 
} 

I then started getting the following errors

cannot open file 'libboost_system-vc100-mt-sgd-1_47.lib'
cannot open file 'libboost_date_time-vc100-mt-sgd-1_47.lib'
cannot open file 'libboost_regex-vc100-mt-sgd-1_47.lib'

In order to resolve these issues I downloaded these files from Link

So I started getting the following error:

Error   12  error LNK1169: one or more multiply defined symbols found   
Error   8   error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)   
Error   10  error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in libcpmtd.lib(newaop.obj)    
Error   9   error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in LIBCMTD.lib(dbgdel.obj) 
Error   11  error LNK2005: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj) 

Any suggestions on what might be going wrong. Do i need to set some input in additional dependencies ?

Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
  • You have not shown the headers included and the command line flags passed to the compiler; Please add them in your question, that will help. The linker error you got is because multiple object files have definitions of the same function. – A. K. Mar 21 '13 at 06:37
  • How do i get the command line flags for VS2010 ? – Rajeshwar Mar 21 '13 at 06:38
  • 1
    Well that's a good question, some Visual Studio user will help you. A simple way to get away with this error would be to uninstall and then re-install the boost library (I'm not sure though). I have learned that re-installing an application really works on windows. – A. K. Mar 21 '13 at 06:40
  • 1
    You should use the same linker and compiler settings in your project as used when building the boost libs you use. That is static linking to the C++ standard library (indicated by the suffix -sgd- in lib name), multithreaded enabled, debug version. – nabulke Mar 21 '13 at 06:44
  • Check this to [decode boost lib naming convention](http://stackoverflow.com/q/2715164/220636) – nabulke Mar 21 '13 at 06:46
  • for the command line flags, right click on your project in visual studio, go to properties, then go to the C++ and Linker sections. Both will have their respective command line entries with the compilation commands used. – alrikai Mar 21 '13 at 07:00

1 Answers1

0

You probably don't need to reinstall Visual Studio. I think the errors you are seeing have to do with a type of versioning problem. The error that you are getting indicates that some components were built with one set of libraries, and other components were built with a different set of libraries. In the error:

error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)   

The last 3 characters of the filename LIBCMTD (MTD) stands for multi-threaded library. If you are building other components that uses this code with a different type of library, for example MT (multi-threaded - no debug), or MDd (multi-threaded debug for a dll) then you will get this error. You might also get this error if you are linking with the wrong set of libraries for Boost. In fact, from the error above the library it is trying to use is:

'libboost_system-vc100-mt-sgd-1_47.lib'

which I believe is the release version. You need to build with the same type of libraries that you are using in your own code. If you are using MTD, then build with the MTD version of Boost libraries as well.

To see what libraries your project(s) are using, right click on the project in the Solution Explorer Window and select properties. Properties can also be selected from the project menu, but make sure that you have a file in that project as the current file open in the editing window before doing this. Under Runtime Library you will see the type of library that you are using. If the project type is a dll, then this value should really be set to MDd. To see what type of project it is, click the Linker (or Librarian) option under properties and if the output file is dll, then the project type is dll. If it says library or exe, then the project is library or exe respectively. All of your projects of a given type should be built the same way. You should not try to mix and match release and debug versions for example. If you have an exe that you are building, then use whatever library that was used to build the library type of projects. To set the library, under properties select Configuration Properties / C/C++ / Code Generation / Runtime Library and choose a type from the dropdown box.

If the configuration looks correct for all projects, then try rebuilding from scratch. This can be done by right clicking on the project and selecting clean, followed by selecting rebuild.

Bob Bryan
  • 3,687
  • 1
  • 32
  • 45