C++: get Boost working; questions on include path and linking library
Most of the boost libraries, are just header files, if you look in the .hpp files you will not see just the declaration of the classes, like you would expect in a header file, but actually the entire implementation. This is why for 90% of the boost libraries, you don't need to worry about linking, only inclusion.
However for a few libraries, serialiser, a few others, there is just too much polluting code for the header inclusion method to be reasonable. I'm sure there is a better, more rigid definition about when the implementation is included in the header and when it isn't.
http://www.boost.org/boost-build2/doc/html/bbv2/faq/header-only-libraries.html
Here is another question about it: Why are not all boost libraries header-only?
p.s. Generally it is better to keep the boost library separate and in your makefile do something like:
For compilation:
CXXFLAGS += -I/path/to/boost/include
For Linking:
LDPATH += -L/path/to/boost/lib
This makes it easier to upgrade your boost version as you just have to change the path in one place.
4ae1e1
Updated on June 05, 2022Comments
-
4ae1e1 over 1 year
I was trying to use Boost.Tokenizer library. In my
PROG.cpp
, I have the following:#include <boost/tokenizer.hpp>
And my
Makefile
was initially something likeCXX = g++-4.8 CXXFLAGS = ## some irrelevant flags LDFLAGS = ## some irrelevant flags SOURCES = PROG.cpp OBJECTS = $(SOURCES:.cpp=.o) TARGETS = PROG $(TARGETS) : $(OBJECTS) $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) ## other targets
It won't compile, since
boost/tokenizer.hpp
cannot be found:fatal error: boost/tokenizer.hpp: No such file or directory
Then I manually added the boost include path to
CXXFLAGS
:-I/opt/local/include/
(which is the path from MacPorts.)
Then I tried to include the Tokenizer library, but in
/opt/local/lib/
I havelibboost_atomic-mt.dylib
,libboost_chrono-mt.dylib
, etc., but nothing liketokenizer
. I was rather confused at the time. I supposed that still wouldn't work since the library was not linked against. Surprisingly, the program built, linked, and ran perfectly.So I'm really confused now. Here are some questions:
(1) I did not link against boost explicitly, so boost is treated like standard library by the linker?
(2) If boost is treated like standard, why the headers are not standard?
(3) Why there are
libboost_atomic-mt.dylib
,libboost_chrono-mt.dylib
, etc. but nottokenizer
? Which dynamic library doestokenizer
belong to?I'm not very familiar with
g++
linking mechanism; speaking of boost, this is my very first program with boost. So I'd really appreciate detailed explanation. Thanks in advance!For reference, this is what I extracted by
gcc -print-search-dirs
:install: /usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/ programs: =/usr/gcc-4.8.0/libexec/gcc/x86_64-apple-darwin12.3.0/4.8.0/:/usr/gcc-4.8.0/libexec/gcc/x86_64-apple-darwin12.3.0/4.8.0/:/usr/gcc-4.8.0/libexec/gcc/x86_64-apple-darwin12.3.0/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/../../../../x86_64-apple-darwin12.3.0/bin/x86_64-apple-darwin12.3.0/4.8.0/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/../../../../x86_64-apple-darwin12.3.0/bin/ libraries: =/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/../../../../x86_64-apple-darwin12.3.0/lib/x86_64-apple-darwin12.3.0/4.8.0/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/../../../../x86_64-apple-darwin12.3.0/lib/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/../../../x86_64-apple-darwin12.3.0/4.8.0/:/usr/gcc-4.8.0/lib/gcc/x86_64-apple-darwin12.3.0/4.8.0/../../../:/lib/x86_64-apple-darwin12.3.0/4.8.0/:/lib/:/usr/lib/x86_64-apple-darwin12.3.0/4.8.0/:/usr/lib/