Eelmises osas tegime hästi pisikese C++ projekti.

Nüüd tahame proovida Boost C++ Libraries tööd. Alustame Visual Studioga. Eelistan lähtekoodi kompileerimast. Laadin alla praeguse viimase versiooni ja pakin lahti asukohta. C:\boost_1_59_0.

Nüüd käivitan Developer Command Prompt for VS2015 ja annan käsklused:

bootstrap
.\b2

Tulemus: 

Kasutame näidet aadressilt https://www.crystax.net/en/blog/2.

Faili gps.cpp panen kausta C:\git-gtest\boostdemo\src:

// gps.cpp
#include <fstream>

#include "gps.h"

const char *FILENAME = "gps.dat";

std::ostream &operator<<(std::ostream &s, gps_position const &g)
{
	s << "GPS(" << g.degrees << "/" << g.minutes << "/" << g.seconds << ")";
	return s;
}

void save(gps_position const &g)
{
	// create and open a character archive for output
	std::ofstream ofs(FILENAME);
	boost::archive::text_oarchive oa(ofs);
	// write class instance to archive
	oa << g;
	// archive and stream closed when destructors are called
}

void load(gps_position &g)
{
	// create and open an archive for input
	std::ifstream ifs(FILENAME);
	boost::archive::text_iarchive ia(ifs);
	// read class state from archive
	ia >> g;
	// archive and stream closed when destructors are called
}

Faili gps.h panen asukohta C:\git-gtest\boostdemo\include:

// gps.hpp
#ifndef GPS_HPP_7D5AF29629F64210BE00F3AF697BA650
#define GPS_HPP_7D5AF29629F64210BE00F3AF697BA650

// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

/////////////////////////////////////////////////////////////
// gps coordinate
//
// illustrates serialization for a simple type
//
class gps_position
{
private:
	friend class boost::serialization::access;
	friend std::ostream &operator<<(std::ostream &, gps_position const &);
	// When the class Archive corresponds to an output archive, the
	// & operator is defined similar to <<.  Likewise, when the class Archive
	// is a type of input archive the & operator is defined similar to >>.
	template<class Archive>
	void serialize(Archive & ar, const unsigned int version)
	{
		ar & degrees;
		ar & minutes;
		ar & seconds;
	}
	int degrees;
	int minutes;
	float seconds;
public:
	gps_position() {}
	gps_position(int d, int m, float s) :
		degrees(d), minutes(m), seconds(s)
	{}

	bool operator==(gps_position const &g) const
	{
		return degrees == g.degrees &&
			minutes == g.minutes &&
			seconds == g.seconds;
	}

	bool operator!=(gps_position const &g) const
	{
		return !(*this == g);
	}
};

void save(gps_position const &g);
void load(gps_position &g);

#endif // GPS_HPP_7D5AF29629F64210BE00F3AF697BA650

Täiendan faili CMakeLists.txt

cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)

set(PROJECT_NAME_STR boostdemo)
project(${PROJECT_NAME_STR} C CXX)
MESSAGE("PROJECT_SOURCE_DIR:${PROJECT_SOURCE_DIR}")
MESSAGE("PROJECT_BINARY_DIR:${PROJECT_BINARY_DIR}")

SET (BOOST_ROOT "C:/boost_1_59_0")
SET (BOOST_INCLUDEDIR "C:/boost_1_59_0/boost")
SET (BOOST_LIBRARYDIR "C:/boost_1_59_0/stage/lib")

SET (BOOST_MIN_VERSION "1.59.0")
set (Boost_NO_BOOST_CMAKE ON)
FIND_PACKAGE(Boost ${BOOST_MIN_VERSION} REQUIRED)
if (NOT Boost_FOUND)
  message(FATAL_ERROR "Fatal error: Boost (version >= 1.59) required.")
else()
  message(STATUS "Setting up BOOST")
  message(STATUS " Includes - ${Boost_INCLUDE_DIRS}")
  message(STATUS " Library  - ${Boost_LIBRARY_DIRS}")
  include_directories(${Boost_INCLUDE_DIRS})
  link_directories(${Boost_LIBRARY_DIRS})
endif (NOT Boost_FOUND)

include_directories(include)
file(GLOB SOURCES src/*.cpp include/*.h)

add_executable(${PROJECT_NAME_STR} ${SOURCES})


set_property(TARGET ${PROJECT_NAME_STR} PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET ${PROJECT_NAME_STR} PROPERTY CXX_STANDARD 14)

Loomulikult peame muutma ka faili main.cpp:

#include <iostream>
#include "main.h"
#include <gps.h>
using namespace std;

int main(int argc, char *argv[]) {
	//cout << "Hello, world !!!" << " \n";
	// create class instance
	const gps_position g(35, 59, 24.567f);
	std::cout << "Initial value: " << g << std::endl;
	save(g);

	// ... some time later restore the class instance to its orginal state
	gps_position newg;
	load(newg);
	std::cout << "After load: " << newg << std::endl;

	if (g != newg)
	{
		std::cerr << "ERROR: Loaded object differs from the saved one" << std::endl;
		return 1;
	}

	std::cout << "Congratulations! GPS object was successfully saved and then loaded" << std::endl;
	return 0;

}

Käsurealt annan uuesti käskluse cmake ..

Nüüd kui Visual Studios uuesti kompileerida, siis näeme oodatud tulemust:

 

Viiteid:

http://www.boost.org/doc/libs/1_59_0/libs/serialization/doc/

https://www.crystax.net/en/blog/2

http://stackoverflow.com/questions/24173330/cmake-is-not-able-to-find-boost-libraries