1

In answer to one of my recent questions, it was suggested that I use a function in boost to solve my problem. This is my first time using boost so I added the following .hpp file

#include <boost/algorithm/string.hpp>

and function call

boost::replace_all(marketPrices, "\\:", "-COLON-");

to my source file, then ran make to build my application using g++ as normal.

At this point I realized I had not added any new library to the link step in my makefile and fully expected the link step to fail. To my surprise it did not fail - not only that but the code ran exactly as it should have done without any complaint.

This surprises me to say the least - how did g++ know what to link to and why did it automatically do so? Or am I missing something fundamental with the way the boost libraries operate? I know that boost uses a lot of templating and this is an aspect of C++ that I am not overly familiar with so I am wondering if perhaps this has something to do with what I am seeing.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
mathematician1975
  • 21,161
  • 6
  • 59
  • 101
  • Boost.StringAlgo is header-only -- no linking involved. – ildjarn Jul 10 '12 at 21:19
  • 1
    If you used Visual Studio, you'll be even more pleasantly surprised to find that even when you use libraries that aren't header only, you *still* don't need to muck around with linker options, because Visual C++ allows you to set linker options through pragmas in your source code, and boost takes advantage of this. – Benjamin Lindley Jul 10 '12 at 21:54

1 Answers1

7

Some of the boost libraries are header-only meaning that there is no binary to link against. Other libraries like boost::thread will require you to add a new lib to the linker.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • To add on: the reason header-only libraries exist is that C++ template code is necessarily totally visible to the compiler, so anything it needs it already has. Much of Boost is templated, so much of Boost is totally visible to the compiler. – GManNickG Jul 10 '12 at 21:22
  • @mathematician1975 Don't feel too stupid, there's lots of weird stuff out there, like [#pragmas for linking with libraries automatically](http://stackoverflow.com/questions/1685206/pragma-commentlib-xxx-lib-equivalent-under-linux). There's a lot of "magic" out there, but you usually only find out about it when it breaks. :) – HostileFork says dont trust SE Jul 10 '12 at 23:37