我正在尝试使用Boost序列化库来对即将开展的项目进行一些实验。我从来没有使用过Boost,如果我误解了某些东西,那就和我一样。
我知道Boost Serialization库必须与其他项目/头文件分开编译。我在Ubuntu 10.04上安装了一个软件包,包括boost1.40-dev和序列化库libboost-serialization1.40.0。
在我的新Net Bean项目中,我尝试了一个简单的例子,看看我是否可以运行。这是一个例子:
/*
* File: main.C
* Author: Doug Swain
*
* Created on January 20, 2011, 11:05 AM
*/
#include <fstream>
#include <iostream>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
int main() {
std::ofstream ofs("filename.xml");
boost::archive::xml_oarchive oa(ofs);
return 0;
}
然后是编译器错误:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/doug/BoostSerialization'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/boostserialization
make[2]: Entering directory `/home/doug/BoostSerialization'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++ -c -g -I/usr/include/boost/serialization -I/usr/include/boost -I/usr/include/boost/archive -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.C
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/boostserialization build/Debug/GNU-Linux-x86/main.o -L/usr/include/boost/serialization -L/usr/include/boost -L/usr/include/boost/archive
build/Debug/GNU-Linux-x86/main.o: In function `xml_oarchive':
/usr/include/boost/archive/xml_oarchive.hpp:100: undefined reference to `boost::archive::xml_oarchive_impl<boost::archive::xml_oarchive>::xml_oarchive_impl(std::basic_ostream<char, std::char_traits<char> >&, unsigned int)'
build/Debug/GNU-Linux-x86/main.o: In function `~xml_oarchive_impl':
/usr/include/boost/archive/xml_oarchive.hpp:73: undefined reference to `boost::archive::basic_xml_oarchive<boost::archive::xml_oarchive>::~basic_xml_oarchive()'
/usr/include/boost/archive/xml_oarchive.hpp:73: undefined reference to `boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::~basic_text_oprimitive()'
/usr/include/boost/archive/xml_oarchive.hpp:73: undefined reference to `boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::~basic_text_oprimitive()'
/usr/include/boost/archive/xml_oarchive.hpp:73: undefined reference to `boost::archive::basic_xml_oarchive<boost::archive::xml_oarchive>::~basic_xml_oarchive()'
/usr/include/boost/archive/xml_oarchive.hpp:73: undefined reference to `boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::~basic_text_oprimitive()'
/usr/include/boost/archive/xml_oarchive.hpp:73: undefined reference to `boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::~basic_text_oprimitive()'
/usr/include/boost/archive/xml_oarchive.hpp:73: undefined reference to `boost::archive::basic_xml_oarchive<boost::archive::xml_oarchive>::~basic_xml_oarchive()'
/usr/include/boost/archive/xml_oarchive.hpp:73: undefined reference to `boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::~basic_text_oprimitive()'
/usr/include/boost/archive/xml_oarchive.hpp:73: undefined reference to `boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::~basic_text_oprimitive()'
build/Debug/GNU-Linux-x86/main.o:(.rodata._ZTIN5boost7archive6detail15common_oarchiveINS0_12xml_oarchiveEEE[typeinfo for boost::archive::detail::common_oarchive<boost::archive::xml_oarchive>]+0x10): undefined reference to `typeinfo for boost::archive::detail::basic_oarchive'
build/Debug/GNU-Linux-x86/main.o: In function `boost::archive::xml_oarchive& boost::archive::detail::interface_oarchive<boost::archive::xml_oarchive>::operator<< <boost::archive::version_type const>(boost::archive::version_type const&)':
/usr/include/boost/archive/detail/interface_oarchive.hpp:64: undefined reference to `boost::archive::basic_xml_oarchive<boost::archive::xml_oarchive>::save_override(boost::archive::version_type const&, int)'
make[2]: Leaving directory `/home/doug/BoostSerialization'
make[1]: Leaving directory `/home/doug/BoostSerialization'
build/Debug/GNU-Linux-x86/main.o: In function `boost::archive::xml_oarchive& boost::archive::detail::interface_oarchive<boost::archive::xml_oarchive>::operator<< <boost::archive::object_id_type const>(boost::archive::object_id_type const&)':
/usr/include/boost/archive/detail/interface_oarchive.hpp:64: undefined reference to `boost::archive::basic_xml_oarchive<boost::archive::xml_oarchive>::save_override(boost::archive::object_id_type const&, int)'
build/Debug/GNU-Linux-x86/main.o: In function `boost::archive::xml_oarchive& boost::archive::detail::interface_oarchive<boost::archive::xml_oarchive>::operator<< <boost::archive::object_reference_type const>(boost::archive::object_reference_type const&)':
/usr/include/boost/archive/detail/interface_oarchive.hpp:64: undefined reference to `boost::archive::basic_xml_oarchive<boost::archive::xml_oarchive>::save_override(boost::archive::object_reference_type const&, int)'
build/Debug/GNU-Linux-x86/main.o: In function `boost::archive::xml_oarchive& boost::archive::detail::interface_oarchive<boost::archive::xml_oarchive>::operator<< <boost::archive::class_id_type const>(boost::archive::class_id_type const&)':
/usr/include/boost/archive/detail/interface_oarchive.hpp:64: undefined reference to `boost::archive::basic_xml_oarchive<boost::archive::xml_oarchive>::save_override(boost::archive::class_id_type const&, int)'
build/Debug/GNU-Linux-x86/main.o: In function `boost::archive::xml_oarchive& boost::archive::detail::interface_oarchive<boost::archive::xml_oarchive>::operator<< <boost::archive::class_id_optional_type const>(boost::archive::class_id_optional_type const&)':
/usr/include/boost/archive/detail/interface_oarchive.hpp:64: undefined reference to `boost::archive::basic_xml_oarchive<boost::archive::xml_oarchive>::save_override(boost::archive::class_id_optional_type const&, int)'
build/Debug/GNU-Linux-x86/main.o: In function `boost::archive::xml_oarchive& boost::archive::detail::interface_oarchive<boost::archive::xml_oarchive>::operator<< <boost::archive::class_id_reference_type const>(boost::archive::class_id_reference_type const&)':
/usr/include/boost/archive/detail/interface_oarchive.hpp:64: undefined reference to `boost::archive::basic_xml_oarchive<boost::archive::xml_oarchive>::save_override(boost::archive::class_id_reference_type const&, int)'
build/Debug/GNU-Linux-x86/main.o: In function `boost::archive::xml_oarchive& boost::archive::detail::interface_oarchive<boost::archive::xml_oarchive>::operator<< <boost::archive::class_name_type const>(boost::archive::class_name_type const&)':
/usr/include/boost/archive/detail/interface_oarchive.hpp:64: undefined reference to `boost::archive::basic_xml_oarchive<boost::archive::xml_oarchive>::save_override(boost::archive::class_name_type const&, int)'
build/Debug/GNU-Linux-x86/main.o: In function `boost::archive::xml_oarchive& boost::archive::detail::interface_oarchive<boost::archive::xml_oarchive>::operator<< <boost::archive::tracking_type const>(boost::archive::tracking_type const&)':
/usr/include/boost/archive/detail/interface_oarchive.hpp:64: undefined reference to `boost::archive::basic_xml_oarchive<boost::archive::xml_oarchive>::save_override(boost::archive::tracking_type const&, int)'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/boostserialization] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
我已经验证了标头确实存在(并且NetBeans可以在尝试构建之前找到它)。我还确保根据我的知识更新包含目录和库目录:
对此的任何想法都会很棒。提前谢谢!
答案 0 :(得分:2)
这些不是编译器错误,而是链接器错误。您需要链接Boost序列化库,可能称为libboost_serialization.a
或类似的东西。
答案 1 :(得分:1)
除了包含来自boost的头文件之外,你必须链接到boost库才能使用它的功能。