Author Topic: Boost.Asio Linking Help  (Read 14542 times)

Offline JakeKazmier

  • Single posting newcomer
  • *
  • Posts: 8
Boost.Asio Linking Help
« on: June 27, 2012, 01:32:08 am »
Hi,
I am trying to setup Boost C++ Libraries but I am having trouble with linking. I installed and compiled the libraries correctly and the first example provided works because it does not require linking. I referred to the BoostWindowsQuickRef and other sources but I could not find what specific files I should link when using a Boost library or where to look for them. I appreciate the help.
Thanks!

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Boost.Asio Linking Help
« Reply #1 on: June 27, 2012, 03:57:51 am »
Do I need to link with a library for every Boost component I use?
Header-only Libraries
Asio dependencies
http://forums.codeblocks.org/index.php/topic,15164.msg101771.html#msg101771
Summary:
  • Boost.System
  • Boost.Regex (optional)
  • OpenSSL (optional)
  • ws2_32 (system - assuming you are on Windows)
(To locate the Boost libraries see Set up a Code::Blocks global variable for Boost step four.)

Also, each library lists its dependencies (somewhere) in its documentation.

Offline dajames

  • Single posting newcomer
  • *
  • Posts: 2
Re: Boost.Asio Linking Help
« Reply #2 on: June 27, 2012, 01:32:18 pm »
It looks like you're using Windows ... if you're using the Microsoft compiler you probably don't need to specify any boost libraries because boost's autolink feature will grab them for you (using #pragma comment( lib ) directives).

If you're not on Windows, or are using GCC on Windows, then autolink is not supported and you will have to specify the library/ies you need explicitly. For boost::asio you need the boost system library and may also need the regex library. If you're building for Linux you'll also need to specify the pthread library.

It's worth comparing the two "getting started" documents to see how the use of boost on Windows and other systems differs.

http://www.boost.org/doc/libs/1_49_0/more/getting_started/windows.html
http://www.boost.org/doc/libs/1_49_0/more/getting_started/unix-variants.html

Offline JakeKazmier

  • Single posting newcomer
  • *
  • Posts: 8
Re: Boost.Asio Linking Help
« Reply #3 on: June 27, 2012, 02:30:12 pm »
Thank you very much.

Offline JakeKazmier

  • Single posting newcomer
  • *
  • Posts: 8
Re: Boost.Asio Linking Help
« Reply #4 on: June 28, 2012, 02:53:23 am »
Actually this does not solve the problem. I get this error if this helps.
Thanks.


Uploaded with ImageShack.us

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Boost.Asio Linking Help
« Reply #5 on: June 28, 2012, 03:26:13 am »
Can you post your full build log?

Offline JakeKazmier

  • Single posting newcomer
  • *
  • Posts: 8
Re: Boost.Asio Linking Help
« Reply #6 on: June 28, 2012, 03:40:18 am »


Uploaded with ImageShack.us
There you go.

Offline JakeKazmier

  • Single posting newcomer
  • *
  • Posts: 8
Re: Boost.Asio Linking Help
« Reply #7 on: June 28, 2012, 03:48:51 am »
This is the coda, all of it. Is it possible that I am running only one part of the full example?
http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/examples.html Chat example.

Code
//
// chat_message.hpp
// ~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef CHAT_MESSAGE_HPP
#define CHAT_MESSAGE_HPP

#include <boost/asio.hpp>
#include <cstdio>
#include <cstdlib>
#include <cstring>

class chat_message
{
public:
  enum { header_length = 4 };
  enum { max_body_length = 512 };

  chat_message()
    : body_length_(0)
  {
  }

  const char* data() const
  {
    return data_;
  }

  char* data()
  {
    return data_;
  }

  size_t length() const
  {
    return header_length + body_length_;
  }

  const char* body() const
  {
    return data_ + header_length;
  }

  char* body()
  {
    return data_ + header_length;
  }

  size_t body_length() const
  {
    return body_length_;
  }

  void body_length(size_t new_length)
  {
    body_length_ = new_length;
    if (body_length_ > max_body_length)
      body_length_ = max_body_length;
  }

  bool decode_header()
  {
    using namespace std; // For strncat and atoi.
    char header[header_length + 1] = "";
    strncat(header, data_, header_length);
    body_length_ = atoi(header);
    if (body_length_ > max_body_length)
    {
      body_length_ = 0;
      return false;
    }
    return true;
  }

  void encode_header()
  {
    using namespace std; // For sprintf and memcpy.
    char header[header_length + 1] = "";
    sprintf(header, "%4d", body_length_);
    memcpy(data_, header, header_length);
  }

private:
  char data_[header_length + max_body_length];
  size_t body_length_;
};

#endif // CHAT_MESSAGE_HPP
« Last Edit: June 28, 2012, 03:54:02 am by JakeKazmier »

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Boost.Asio Linking Help
« Reply #8 on: June 28, 2012, 04:04:26 am »
From what I can tell, you did not add the libraries previously mentioned to your linker.
The ones you need are system (most likely named something similar to boost_system-mgw44-mt-1_49) and ws2_32.
See here if you do not know how to add libraries.

For future reference, that is not a full build log.  A full build log would look something like this.  (See my previous link for how to enable full command line logging.)
Code
-------------- Build: Release in Hello ---------------
mingw32-g++.exe -Wall -fexceptions -Os -c "C:\Documents and Settings\Alpha\My Documents\Programing\Hello\main.cpp" -o obj\Release\main.o
mingw32-g++.exe -o bin\Release\Hello.exe obj\Release\main.o -s
Output size is 7.00 KB
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings (0 minutes, 0 seconds)

Offline JakeKazmier

  • Single posting newcomer
  • *
  • Posts: 8
Re: Boost.Asio Linking Help
« Reply #9 on: June 28, 2012, 02:24:10 pm »
So i added the "ws2_32" but in the i get an error saying that it cant find "boost_system-mgw44-mt-1_49". I found the dependencies but it did not list "ws2_32" any idea why? I know c++ but this is my first time working with boost or any 3rd party library for that matter, and I did quite some research on how to link libraries, but sorry if I seem clueless.
For future reference though, is there a file I should be looking for? And how do I know what version and numbers etc. to use in the linker code? One more thing... how do i know what i need for each specific library?
Thanks.
« Last Edit: June 28, 2012, 02:29:23 pm by JakeKazmier »

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Boost.Asio Linking Help
« Reply #10 on: June 29, 2012, 03:41:20 am »
I found the dependencies but it did not list "ws2_32" any idea why?
It is rather cryptic.
Quote
When compiling for Windows using Microsoft Visual C++ or Borland C++, Boost.Asio will automatically link in the necessary Windows SDK libraries for sockets support (i.e. ws2_32.lib and mswsock.lib, or ws2.lib when building for Windows CE). The BOOST_ASIO_NO_DEFAULT_LINKED_LIBS macro prevents these libraries from being linked.
Which means that on Windows, you need Windows Sockets API, which, in MinGW, is available through ws2_32.

For future reference though, is there a file I should be looking for?
Normally, libraries for MinGW (and GCC in general) will be named in the form of lib*.a - when specifying them to the linker, the lib prefix and .a extension are not necessary.  Wherever you installed boost to (assuming you compiled the libraries), there should be a folder with a bunch of libboost_*-mgw*-*-1_*.a files (the asterisks depend on your compiler, the version of boost you downloaded, and the configuration(s) you built).  This folder needs to be in your linker search directories.  The one with "system" in its name is the one you are trying to link to.

Offline JakeKazmier

  • Single posting newcomer
  • *
  • Posts: 8
Re: Boost.Asio Linking Help
« Reply #11 on: June 29, 2012, 10:35:34 pm »
Thanks, I found the files but it is still not working, I must be missing something. Here are some pictures to show you what I have...
Thanks



Uploaded with ImageShack.us


Uploaded with ImageShack.us


Uploaded with ImageShack.us


Uploaded with ImageShack.us

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Boost.Asio Linking Help
« Reply #12 on: June 30, 2012, 03:46:22 am »
This linker error is a result of trying to compile an executable without a main method ( int main(int argc, char* argv[]){ ... } ).  If you do have a main(), go to Project->Properties...->Build targets (tab) and ensure that you have selected "Console application" for the type (not "GUI application").

(By the way, try Google/your favorite search engine occasionally; keywords from this error yield a lot of results.  Also, you are probably tempting admins to lock this thread due to irrelevance to Code::Blocks itself - some of your questions belong on general programming forums - search the web, there are plenty of them.)

Offline JakeKazmier

  • Single posting newcomer
  • *
  • Posts: 8
Re: Boost.Asio Linking Help
« Reply #13 on: June 30, 2012, 09:34:49 pm »
That does not work... When I search the error online nothing relevant appears. But what if the error is not caused by linking, but by my incompetence. In one of my above posts I give a link to the example I am using with Boost.asio. I know the key c++ concepts and the basics on using codeblocks, but I haven't seriously programmed with c++, does that make sense? So maybe im compiling the example wrong because they have three projects that should run together I guess... I am really stumped on how to use this. And about finding the solution to the problem online: crazy. There is no clear solution to the problem, a lot of people use a different IDE, others not the same library, its a mess. If anyone could help I would really appreciate it.



Uploaded with ImageShack.us


I tried to compile the example that they gave for filesystem and THAT doesn't work. Very irritating. Here is a pic of that compile if its of use...



Uploaded with ImageShack.us

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7591
    • My Best Post
Re: Boost.Asio Linking Help
« Reply #14 on: July 01, 2012, 05:58:16 am »
Turn on Full Compiler Logging. http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28errors%29#Q:_How_do_I_troubleshoot_a_compiler_problem.3F

Then, I suggest posting on a different website.

Pick one that supports your Compiler and/or the library that is causing the linking issue.

Remember when linking the order order of the library/object files matter with most compilers/linkers.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org