While using
CMake to compile a simple c++ code containing boost library utilization, I encountered the following linkage problem:
main.cpp:(.text._ZN5boost9iostreams6detail9close_allINS0_21basic_gzip_compressorISaIcEEENS1_16linked_streambufIcSt11char_traitsIcEEEEEvRT_RT0_[void
boost::iostreams::detail::close_all
>, boost::iostreams::detail::linked_streambuf > >(boost::iostreams::basic_gzip_compressor
>&, boost::iostreams::detail::linked_streambuf >&)]+0x18e): undefined reference to
`boost::iostreams::detail::zlib_base::reset(bool, bool)'
collect2: ld returned 1 exit status
make[2]: *** [test.exe] Error 1
make[1]: *** [CMakeFiles/test.exe.dir/all] Error 2
make: *** [all] Error 2
For reference, the code I was trying to compile was the following ( a simple zipping and reading from a zipped file).:
#include <fstream>
#include <iostream>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
using namespace std;
using namespace boost::iostreams;
int main()
{
//The following code decompresses data from a file and writes it to standard output.
ifstream file("hello.txt.gz", ios_base::in | ios_base::binary);
filtering_streambuf<input> in;
in.push(gzip_decompressor());
in.push(file);
boost::iostreams::copy(in, cout);
boost::iostreams::filtering_ostream geometry;
geometry.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip::best_compression));
// Generating a Zip file
string filename = "example1.txt.gz";
filtering_ostream out;
unsigned int compressionLevel = boost::iostreams::gzip::best_compression;
out.push(gzip_compressor(compressionLevel));
out.push(file_sink(filename.c_str(), std::ios::binary));
out << "This is a gz file\n";
out << "This is the second line\n";
}
How I solved this problem is as follows:
1) I already had the following setting in the CMakeList.txt file:
SET(BOOST_DIR /usr/intel/pkgs/boost/1.54.0-gcc-4.7.2 CACHE PATH "Boost directory")
2) Then, I added the bold and red lines below to have it compile without linkage error (the black lines already existed)
SET(BOOST_LIBDIR ${BOOST_DIR}/lib64)
SET(BOOST_LIB -L${BOOST_LIBDIR} boost_thread.a pthread.a )
SET(BOOST_INCLUDE_DIR ${BOOST_DIR}/include )
LINK_DIRECTORIES(${BOOST_LIBDIR})
INCLUDE_DIRECTORIES(${BOOST_INCLUDE_DIR} )
find_package(Boost COMPONENTS system filesystem iostreams regex REQUIRED)
set (EXE test.exe)
add_executable(${EXE} main.cpp )
TARGET_LINK_LIBRARIES(${EXE} ${Boost_LIBRARIES} )
This made the trick and it worked without problems