Linker error with Boost Serialization using a particular class in a test executable, but not in production executable - boost

I'm using Boost 1.49, OpenCV 2.3.1, CMake 2.8 in a C++ project.
I'm building two executables with the same header file, one for production and the other for testing. The contest.hpp file specifies:
namespace boost
{
namespace serialization
{
template<class Archive>
void serialize(Archive& ar, cv::Point& p, const unsigned int version);
template<class Archive>
void serialize(Archive& ar, cv::Rect_<int>& r, const unsigned int version);
template<class Archive>
void serialize(Archive& ar, cv::Size_<int>& s, const unsigned int version);
}
}
and these are implemented in contest.cpp file like
namespace boost
{
namespace serialization
{
template<class Archive>
void serialize(Archive& ar, cv::Point& p, const unsigned int version)
{
ar & p.x;
ar & p.y;
}
template<class Archive>
void serialize(Archive& ar, cv::Rect_<int>& r, const unsigned int version)
{
ar & r.x;
ar & r.y;
ar & r.height;
ar & r.width;
}
template<class Archive>
void serialize(Archive& ar, cv::Size_<int>& s, const unsigned int version)
{
ar & s.width;
ar & s.height;
}
}
}
And test-contest.cpp mundanely tests these like
BOOST_AUTO_TEST_CASE(Serialization1)
{
Point point_write(3, 5);
{
ofstream point_file("Point.txt");
ar::text_oarchive point_archive(point_file);
sr::serialize(point_archive, point_write, 0);
}
Point point_read(0, 0);
{
ifstream point_file("Point.txt");
ar::text_iarchive point_archive(point_file);
sr::serialize(point_archive, point_read, 0);
}
BOOST_CHECK(point_read == point_write);
Rect rect_write(3, 5, 1, 7);
{
ofstream rect_file("Rect.txt");
ar::text_oarchive rect_archive(rect_file);
sr::serialize(rect_archive, rect_write, 0);
}
Rect rect_read(0, 0, 0, 0);
{
ifstream rect_file("Rect.txt");
ar::text_iarchive rect_archive(rect_file);
sr::serialize(rect_archive, rect_read, 0);
}
BOOST_CHECK((rect_read.x) == (rect_write.y) && (rect_read.y) == (rect_write.y));
Size size_write(3, 5);
{
ofstream size_file("Size.txt");
ar::text_oarchive size_archive(size_file);
sr::serialize(size_archive, size_write, 0);
}
Size size_read(0, 0);
{
ifstream size_file("Size.txt");
ar::text_iarchive size_archive(size_file);
sr::serialize(size_archive, size_read, 0);
}
BOOST_CHECK(size_read == size_write);
}
where sr and ar are defined in common.hpp like
namespace ar = ::boost::archive;
namespace sr = ::boost::serialization;
The makefiles are produced from CMake configuration like:
link_directories(${DEPENDENCY_DIR}/libboost/lib)
link_directories(${DEPENDENCY_DIR}/libopencv/lib)
add_executable(test-contest test-contest.cpp contest.cpp)
add_executable(contest main.cpp contest.cpp)
target_link_libraries(test-contest boost_serialization opencv_core opencv_highgui opencv_imgproc boost_program_options boost_unit_test_framework boost_filesystem)
target_link_libraries(contest boost_serialization opencv_core opencv_highgui opencv_imgproc boost_program_options boost_filesystem)
so, the moral of the story is, both contest and test-contest are compiled and linked against the same header and libraries. Note that other Boost libraries are linked and tested successfully.
When it comes to make after cmake 'Unix Makefiles' .., for contest it says
[ 40%] Building CXX object CMakeFiles/contest.dir/main.cpp.o
[ 60%] Building CXX object CMakeFiles/contest.dir/contest.cpp.o
Linking CXX executable contest
[ 60%] Built target contest
but for test-contest
[ 80%] Building CXX object CMakeFiles/test-contest.dir/test-contest.cpp.o
[100%] Building CXX object CMakeFiles/test-contest.dir/contest.cpp.o
Linking CXX executable test-contest
CMakeFiles/test-contest.dir/test-contest.cpp.o: In function `otap::TPageImage::Serialization1::test_method()':
/home/iesahin/Repository/otapexplorer/cli/contest-2012/test-contest.cpp:119: undefined reference to `void boost::serialization::serialize<boost::archive::text_oarchive>(boost::archive::text_oarchive&, cv::Rect_<int>&, unsigned int)'
/home/iesahin/Repository/otapexplorer/cli/contest-2012/test-contest.cpp:125: undefined reference to `void boost::serialization::serialize<boost::archive::text_iarchive>(boost::archive::text_iarchive&, cv::Rect_<int>&, unsigned int)'
/home/iesahin/Repository/otapexplorer/cli/contest-2012/test-contest.cpp:133: undefined reference to `void boost::serialization::serialize<boost::archive::text_oarchive>(boost::archive::text_oarchive&, cv::Size_<int>&, unsigned int)'
/home/iesahin/Repository/otapexplorer/cli/contest-2012/test-contest.cpp:139: undefined reference to `void boost::serialization::serialize<boost::archive::text_iarchive>(boost::archive::text_iarchive&, cv::Size_<int>&, unsigned int)'
collect2: ld returned 1 exit status
make[2]: *** [test-contest] Error 1
And IMHO the weirdest part is, there are no errors for serialize(Archive, cv::Point, int) which is from the same header/library in OpenCV with serialize(Archive, cv::Rect_<int>, int) or cv::Size_<int>. Their serialization/test functions are almost identically defined.
I'm linking these serialize functions from contest.cpp file successfully. I tried replacing Rect with Rect_<int> in the definitions, to no change. I tried changing the order of libraries in CMakeLists.txt and took boost_serialization from the end to its current place, to no avail.
Where am I doing it wrong?

I think you need the implementations of your three serialize functions moved from contest.cpp to contest.hpp.
For the reasons why, see Why can templates only be implemented in the header file? or Why should the implementation and the declaration of a template class be in the same header file?

Related

Qt boost serialize compile errors on simple implementation

Platform specifics:
Fedora 21
g++ 4.9.2-6
Here's the error ....
$ g++ -c kabi-serial.cpp -lboost_serialization
kabi-serial.cpp: In function ‘void boost::serialization::kb_write_list()’:
kabi-serial.cpp:41:13: error: expected ‘;’ before ‘oa’
serialize oa(ofs);
kabi-serial.cpp:41:20: error: statement cannot resolve address of overloaded function
serialize oa(ofs);
^
kabi-serial.cpp:42:3: error: ‘oa’ was not declared in this scope
oa << ql;
And here's the kabi-serial.cpp source ...
#include <boost/serialization/vector.hpp>
#include <vector>
namespace boost {
namespace serialization {
class Cqnodelist
{
public:
friend class boost::serialization::access;
Cqnodelist(){}
std::vector<int>qnodelist;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & qnodelist;
}
};
void kb_write_list ()
{
Cqnodelist ql;
ofstream ofs("kabi-list.dat");
{
serialize oa(ofs);
oa << ql;
}
}
}
}
I based the source code on the simplest of the serialization examples, except using the serialization of the stl vector object.
I'm sure it's simple, but what am I missing?
To use ofstream you need to include <fstream>.
ofstream is in the namespace std: std::ofstream.
You don't need to put your code inside boost::serialization namespace (except case when you implement external to class serialize() function).
Adding serialize method implements way of loading and saving object attributes.
To actually store or load data you need to select storage — archive in Boost.Serialization terminology.
#include <boost/serialization/vector.hpp>
// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <vector>
#include <fstream> // required for std::ofstream
class Cqnodelist
{
public:
friend class boost::serialization::access;
Cqnodelist(){}
std::vector<int>qnodelist;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & qnodelist;
}
};
void kb_write_list ()
{
Cqnodelist ql;
// fill object with data
ql.qnodelist.push_back(1);
ql.qnodelist.push_back(2);
ql.qnodelist.push_back(3);
// See http://www.boost.org/doc/libs/1_57_0/libs/serialization/doc/tutorial.html
// for reference
// save data to archive
{
// open file that will contain serialized data
std::ofstream ofs("kabi-list.dat");
// create archive on top of opened file
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
oa << ql;
}
// to load object later open file, create input archive on top of opened
// file and load object state
{
Cqnodelist new_ql;
// create and open an archive for input
std::ifstream ifs("kabi-list.dat");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> new_ql;
}
}
int main()
{
kb_write_list();
}

How can I remove the warning that my iterator has a non-virtual destructor when extending boost::iterator_facade?

When compiling with -Weffc++ and extending boost::iterator_facade, I get the compiler warning: base class has a non-virtual destructor. What can I do to fix this?
Here is sample code:
#include <iostream>
#include <boost/iterator/iterator_facade.hpp>
struct my_struct_t {
int my_int;
my_struct_t() : my_int(0) {
}
};
class my_iterator_t : public boost::iterator_facade<
my_iterator_t,
my_struct_t const,
boost::forward_traversal_tag
> {
private:
friend class boost::iterator_core_access;
my_struct_t my_struct;
public:
my_iterator_t() : my_struct() {
}
void increment() {
++ my_struct.my_int;
}
bool equal(my_iterator_t const& other) const {
return this->my_struct.my_int == other.my_struct.my_int;
}
my_struct_t const& dereference() const {
return my_struct;
}
};
int main() {
my_iterator_t my_iterator;
std::cout << my_iterator->my_int << "\n";
++my_iterator;
std::cout << my_iterator->my_int << "\n";
return 0;
}
I compile on Fedora 19 like this:
$ g++ test.cpp -std=gnu++0x -Weffc++ -o test
Here is the actual warning:
g++ test.cpp -std=gnu++0x -Weffc++ -o test
test.cpp:10:7: warning: base class ‘class boost::iterator_facade<my_iterator_t, const my_struct_t, boost::forward_traversal_tag>’ has a non-virtual destructor [-Weffc++]
class my_iterator_t : public boost::iterator_facade<
^
Thanks.
-Weffc++ option enables warnings about violations of the some style guidelines from Scott Meyers’ Effective C++ book. Your code violates the Item 7: Make destructors virtual in polymorphic base classes. So the compiler isn't complaining about your iterator, it's about the base class: boost::iterator_facade. I don't think you can eliminate the warning by modify your own code. As to why virtual destructor in polymorphic base classes are so important, a good answer is here.

can't use static std::atomic (and don't know how to initialize it)

I have the following code:
#include <cstdlib>
#include <cstdio>
#include <atomic>
enum ATYPE { Undefined = 0, typeA, typeB, typeC };
template<ATYPE TYPE = Undefined>
struct Object
{
Object() { counter++; }
static std::atomic<int> counter;
};
//template<ATYPE TYPE>
//std::atomic<int> Object<TYPE>::counter = 0;
template<ATYPE TYPE>
void test()
{
printf("in test\n");
Object<TYPE> o;
}
int main(int argc, char **argv)
{
test<typeA>();
printf("%d\n", Object<typeA>::counter.load());
return 0;
}
and when I compile it with the following command line:
clang++ -o test -std=c++11 -stdlib=libc++ test.cpp
I got the following error:
Undefined symbols for architecture x86_64:
"Object<(ATYPE)1>::counter", referenced from:
_main in testray-D4iTOH.o
Object<(ATYPE)1>::Object() in testray-D4iTOH.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have no idea if what I am trying to do is technically possible. As the code hopefully shows, I am trying to create a static instance of the atomic class (BTW, I have no idea how to initialize this variable either. How do you intialize a static std::atomic<>?). What I am trying to do is count the number of instances of the class Object created while running the program, for each possible type (typeA, B, C, etc.).
That's the mechanism I came up with but maybe (beside the problem I have which I would like to fix if possible) someone could advice a better solution? It would be much appreciated.
Thank you so much.
As pointed by Dave in the comment, the static variable needs to be declared somewhere:
include
#include <cstdio>
#include <atomic>
enum ATYPE { Undefined = 0, typeA, typeB, typeC };
template<ATYPE TYPE = Undefined>
struct Object
{
Object() { counter++; }
static std::atomic<int> counter;
};
template<ATYPE TYPE>
std::atomic<int> Object<TYPE>::counter(0);
template<ATYPE TYPE>
void test()
{
printf("in test\n");
Object<TYPE> o;
}
int main(int argc, char **argv)
{
test<typeA>();
printf("%d\n", Object<typeA>::counter.load());
return 0;
}
It compiles fine.

compiling/linking template class as static library

I'm working on a program right now and to test template classes (which I will need) I wrote a small (and buggy, chances are 2 or 3 logic bugs in it, my goal is to get it to compile) stack class. What I want to do is to compile it to a static library (.a) then link it with the main program.
The error is:
main.cpp:(.text+0x1c): undefined reference to `Stack<int>::Stack()'
main.cpp:(.text+0x31): undefined reference to `Stack<int>::push(int)'
main.cpp:(.text+0x42): undefined reference to `Stack<int>::push(int)'
main.cpp:(.text+0x4e): undefined reference to `Stack<int>::pop()'
main.cpp:(.text+0x5d): undefined reference to `Stack<int>::pop()'
collect2: error: ld returned 1 exit status
This is the header file:
/* stack.h */
#ifndef _STACK_INCLUDED_
#define _STACK_INCLUDED_
template<typename T>
struct Node
{
T* node;
Node<T>* next;
};
template<typename T>
class Stack
{
private:
Node<T>* bottom;
public:
Stack();
Stack(T first);
Stack(T* arr, int amount);
void push(T push);
T* pop();
};
/* I added the following prototypes in an attempt to correct the error,
did not work*/
template<typename T>
Stack<T>::Stack();
template<typename T>
Stack<T>::Stack(T first);
template<typename T>
Stack<T>::Stack(T* arr, int amount);
template<typename T>
void Stack<T>::push(T push);
template<typename T>
T* Stack<T>::pop();
#endif
Here is the implementation file:
/* stack.cpp */
#include "../heads/stack.h"
#define null (void*)0
template<typename T>
Stack<T>::Stack() {
bottom = null;
}
template<typename T>
Stack<T>::Stack(T first) {
push(first);
}
template<typename T>
Stack<T>::Stack(T* arr, int amount)
{
int i;
for(i=0;i<amount; i++)
{
push(arr[i]);
}
}
template<typename T>
void Stack<T>::push(T push)
{
Node<T>* tmp = new Node<T>();
tmp->node = push;
tmp->next = null;
Node<T>* node = bottom;
while(node->next != null)
{
node = node->next;
}
node->next = tmp;
}
template<typename T>
T* Stack<T>::pop()
{
int i=0;
Node<T>* node = bottom;
while(node->next != null)
{
i++;
node = node->next;
}
node = bottom;
for(;i>1;i++)
{
node = node->next;
}
Node<T>* res = node->next;
node->next = null;
return res->node;
}
You might have noticed the header file is included: "../heads/stack.h", this is because the structure looks like so:
- root
-- CLASSNAME
--- implementation of CLASSNAME
-- heads
--- all the headers
-- obj
--- object files (compiled)
--bin
---final output
.
The makefile looks like so:
CC=g++
CFLAGS=-c -fpermissive -Wall
LFLAGS=-llua
OBJ=obj
BIN=bin
HS=heads
all: $(OBJ)/bind.a $(OBJ)/stack.a $(OBJ)/main.o
$(CC) $(LFLAGS) -o $(BIN)/main $(OBJ)/main.o $(OBJ)/bind.a $(OBJ)/stack.a
$(OBJ)/bind.o: binds/bind.cpp $(HS)/bind.h
$(CC) $(CFLAGS) binds/bind.cpp -o $(OBJ)/bind.o
$(OBJ)/bind.a: $(OBJ)/bind.o
ar -cvq $(OBJ)/bind.a $(OBJ)/bind.o
$(OBJ)/main.o:
$(CC) $(CFLAGS) main/main.cpp -o $(OBJ)/main.o
$(OBJ)/stack.o: $(HS)/stack.h stack/stack.cpp
$(CC) $(CFLAGS) stack/stack.cpp -o $(OBJ)/stack.o
$(OBJ)/stack.a: $(OBJ)/stack.o
ar -cvq $(OBJ)/stack.a $(OBJ)/stack.o
clean:
touch $(OBJ)/dummy
rm $(OBJ)/*
Compiler: g++ 4.7.2 (gcc-multilib)
OS: Arch Linux (x86_64) (kernel 3.6.6-1)
You can get the whole file here (I'm doing multiple test, so don't mind the -llua flag and other files, you can change the Makefile if needed, I just want to figure this out).
After some research and testing, I noticed that the symbols aren't being exported, (nm obj/stack.o shows nothing, while nm obj/bind.o does. same thing for (.a)).
However I could not find any reference to what to do in this case.
You can't make a lib for every possible template parameters, but you can for some types.
A templated class is used to generate code of a class that taking the given type.
When there is a instance using it, it will generate the actual class, otherwise, it will be simply omitted when compile.
According to Compile header-only template library into a shared library? this is possible by using Explicit Instantiation, and make the types you declared into the lib, and let the rest to generate in compile.

Compilation error when using boost serialization library

I have been struggling with this error for a long time.
The following is my code snippet.
//This is the header file
template<typename TElem>
class ArrayList {
public:
/** An accessible typedef for the elements in the array. */
typedef TElem Elem;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & ptr_;
ar & size_;
ar & cap_;
}
Elem *ptr_; // the stored or aliased array
index_t size_; // number of active objects
index_t cap_; // allocated size of the array; -1 if alias
};
template <typename TElem>
class gps_position
{
public:
typedef TElem Elem;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & degrees;
ar & minutes;
ar & seconds;
}
private:
Elem degrees;
index_t minutes;
index_t seconds;
};
// This is the .cc file
#include <string>
#include <fstream>
#include <iostream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/serialization.hpp>
#include "arraylist.h"
int main() {
// create and open a character archive for output
std::ofstream ofs("filename");
// create class instance
// gps_position<int> g(35.65, 59, 24.567f);
gps_position<float> g;
// save data to archive
{
boost::archive::text_oarchive oa(ofs);
// write class instance to archive
//oa << g;
// archive and stream closed when destructors are called
}
// ... some time later restore the class instance to its orginal state
/* gps_position<int> newg;
{
// create and open an archive for input
std::ifstream ifs("filename");
boost::archive::text_iarchive ia(ifs);
// read class state from archive
ia >> newg;
// archive and stream closed when destructors are called
}*/
ArrayList<float> a1;
ArrayList<int> a2;
a1.Init(22);
a2.Init(21);
// a1.Resize(30);
// a1.Resize(12);
// a1.Resize(22);
// a2.Resize(22);
a1[21] = 99.0;
a1[20] = 88.0;
for (index_t i = 0; i < a1.size(); i++) {
a1[i] = i;
a1[i]++;
}
std::ofstream s("test.txt");
{
boost::archive::text_oarchive oa(s);
oa << a1;
}
return 0;
}
The following is the compilation error i get.
In file included from /usr/include/boost/serialization/split_member.hpp:23,
from /usr/include/boost/serialization/nvp.hpp:33,
from /usr/include/boost/serialization/serialization.hpp:17,
from /usr/include/boost/archive/detail/oserializer.hpp:61,
from /usr/include/boost/archive/detail/interface_oarchive.hpp:24,
from /usr/include/boost/archive/detail/common_oarchive.hpp:20,
from /usr/include/boost/archive/basic_text_oarchive.hpp:32,
from /usr/include/boost/archive/text_oarchive.hpp:31,
from demo.cc:4:
/usr/include/boost/serialization/access.hpp: In static member function ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = float]’:
/usr/include/boost/serialization/serialization.hpp:74: instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = float]’
/usr/include/boost/serialization/serialization.hpp:133: instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = float]’
/usr/include/boost/archive/detail/oserializer.hpp:140: instantiated from ‘void boost::archive::detail::oserializer<Archive, T>::save_object_data(boost::archive::detail::basic_oarchive&, const void*) const [with Archive = boost::archive::text_oarchive, T = float]’
demo.cc:105: instantiated from here
/usr/include/boost/serialization/access.hpp:109: error: request for member ‘serialize’ in ‘t’, which is of non-class type ‘float’
Please help me out.
You can't serialize a raw pointer to a float, which is what you're trying to do here. In fact you're not actually serializing the array anyway when you try to serialize a1 - you only try to serialize the pointer to the head of it.
I don't know what you're trying to accomplish, but can you just make a1 and a2 raw float arrays? Boost can serialize those natively.
float a1[21];
a1[21] = 99.0;
for (index_t i = 0; i < 21; i++) {
a1[i] = i;
a1[i]++;
}
std::ofstream s("test.txt");
boost::archive::text_oarchive oa(s);
oa << a1;
The better option is probably to change your ArrayList definition to use std::vector<Elem> instead of a raw Elem *. Boost::Serialization also already knows how to serialize all STL containers.
For further information see the Boost serialization reference manual.

Resources