Note to self: If relying on implicit make rules, then the libraries you want to link to need to go into the LDLIBS variable, not in the LDFLAGS variable.

The case at hand: I wanted to do a quick test on how to write gzipped files using the Boost libraries. Because this was a simple example, I also wanted a simple Makefile to accompany it, meaning I wanted to use implicit rules.

Here’s the example C++ code I used, slightly modified from the Boost example:

#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
 
namespace io = boost::iostreams;
int main()
{
    using namespace std;
 
    ifstream infile("hello.txt", ios_base::in | ios_base::binary);
    ofstream outfile("hello.txt.gz", ios_base::out | ios_base::binary);
    io::filtering_streambuf<io::output> out;
    out.push(io::gzip_compressor());
    out.push(outfile);
    io::copy(infile, out);
 
    return 0;
}

The accompanying Makefile looks like this:

CXXFLAGS=-I/usr/include/boost
LDLIBS=-lboost_iostreams -lboost_system -lstdc++
 
# Needed because otherwise cc is used, in which case -lstdc++
# must be added to -LDLIBS
#CC=g++
 
PROGRAM=boost_write_gzip
 
$(PROGRAM): $(PROGRAM).o
 
clean:
	$(RM) $(PROGRAM).o $(PROGRAM)

Notes

  • Note the addition of -lstdc++ to the LDLIBS, this is because the implicit rule uses cc to do the linking. This is no problem for C++ code, as longs as you add the C++ standard library. Alternatively, you can set CC=g++ as shown in the comment, instead of adding -lstdc++.
  • Note that somewhere since Boost v1.50 the addition of -lboost_system is required.
  • This was done on a machine with Ubuntu Linux 13.10 installed, Boost version 1.53 (the libboost-all-dev package).

Links:

Related Images: