在Linux环境下,C++序列化有多种方法,Boost.Serialization功能全面,支持复杂数据类型与指针关系。通过包管理器安装Boost库,结合文本存档实现序列化与反序列化,示例展示了Person类对象的存储与加载。该方法适用于对象持久化与网络传输,需注意版本兼容性。
C++在Linux中实现序列化的方法
作者:技术拾光
时间:2026-08-14
浏览:481
文章目录
在Linux环境下,用C++做序列化其实有很多种选择——从功能全面的第三方库,到简单直接的标准库文件操作,各有各的适用场景。下面就把几种常见的方法逐一梳理一下,方便你在实际项目中按需选用。

1. 使用Boost.Serialization
Boost.Serialization 算是这一领域的老牌选手了,功能非常全面,支持各种复杂数据类型的序列化和反序列化。如果你需要处理嵌套容器、继承体系或者指针关系,它基本都能搞定。
安装Boost库
在大多数Linux发行版里,直接用包管理器安装就好:
sudo apt-get install libboost-all-dev
示例代码
#include
#include
#include
#include
#include
#include
class Person {
public:
std::string name;
int age;
template
void serialize(Archive & ar, const unsigned int version) {
ar & name;
ar & age;
}
};
int main() {
std::vector people = {{"Alice", 30}, {"Bob", 25}};
// 序列化到文件
{
std::ofstream ofs("people.txt");
boost::archive::text_oarchive oa(ofs);
oa << people;
}
// 从文件反序列化
std::vector loaded_people;
{
std::ifstream ifs("people.txt");
boost::archive::text_iarchive ia(ifs);
ia >> loaded_people;
}
// 打印反序列化后的数据
for (const auto& person : loaded_people) {
std::cout << person.name << " " << person.age << std::endl;
}
return 0;
}
2. 使用Cereal
Cereal 走的是轻量级路线,头文件只有几十个,不需要编译成库文件,直接包含头文件就能用。它的API简洁,性能也不错,在不少现代C++项目中很受欢迎。
安装Cereal库
从GitHub上克隆下来,然后用CMake编译安装:
git clone https://github.com/USCiLab/cereal.git
cd cereal
mkdir build && cd build
cmake ..
make
sudo make install
示例代码
#include
#include
#include
#include
#include
class Person {
public:
std::string name;
int age;
template
void serialize(Archive & archive) {
archive(name, age);
}
};
int main() {
std::vector people = {{"Alice", 30}, {"Bob", 25}};
// 序列化到文件
{
std::ofstream ofs("people.json");
cereal::JSONOutputArchive oarchive(ofs);
oarchive(people);
}
// 从文件反序列化
std::vector loaded_people;
{
std::ifstream ifs("people.json");
cereal::JSONInputArchive iarchive(ifs);
iarchive(loaded_people);
}
// 打印反序列化后的数据
for (const auto& person : loaded_people) {
std::cout << person.name << " " << person.age << std::endl;
}
return 0;
}
3. 使用标准库(如std::ofstream和std::ifstream)
如果业务场景足够简单,比如只需要记录一些结构化文本或二进制数据,那直接用标准库的文件流就能解决,完全不需要引入任何第三方依赖。
示例代码(文本序列化)
#include
#include
#include
#include
struct Person {
std::string name;
int age;
};
int main() {
std::vector people = {{"Alice", 30}, {"Bob", 25}};
// 序列化到文件
{
std::ofstream ofs("people.txt");
for (const auto& person : people) {
ofs << person.name << "," << person.age << std::endl;
}
}
// 从文件反序列化
std::vector loaded_people;
{
std::ifstream ifs("people.txt");
std::string line;
while (std::getline(ifs, line)) {
size_t pos = line.find(',');
Person person;
person.name = line.substr(0, pos);
person.age = std::stoi(line.substr(pos + 1));
loaded_people.push_back(person);
}
}
// 打印反序列化后的数据
for (const auto& person : loaded_people) {
std::cout << person.name << " " << person.age << std::endl;
}
return 0;
}
示例代码(二进制序列化)
#include
#include
#include
#include
struct Person {
std::string name;
int age;
};
int main() {
std::vector people = {{"Alice", 30}, {"Bob", 25}};
// 序列化到文件
{
std::ofstream ofs("people.bin", std::ios::binary);
for (const auto& person : people) {
size_t name_size = person.name.size();
ofs.write(reinterpret_cast(&name_size), sizeof(name_size));
ofs.write(person.name.c_str(), name_size);
ofs.write(reinterpret_cast(&person.age), sizeof(person.age));
}
}
// 从文件反序列化
std::vector loaded_people;
{
std::ifstream ifs("people.bin", std::ios::binary);
while (ifs.good()) {
Person person;
size_t name_size;
ifs.read(reinterpret_cast(&name_size), sizeof(name_size));
person.name.resize(name_size);
ifs.read(&person.name[0], name_size);
ifs.read(reinterpret_cast(&person.age), sizeof(person.age));
loaded_people.push_back(person);
}
}
// 打印反序列化后的数据
for (const auto& person : loaded_people) {
std::cout << person.name << " " << person.age << std::endl;
}
return 0;
}
这三种方法各有千秋:Boost.Serialization 功能最全但包体积偏大,Cereal 轻巧现代,标准库方案则适合快速原型或对依赖要求严苛的场景。选择哪条路,取决于你对性能、易用性和兼容性的实际权衡。
实用工具推荐
电子包浆
使用 ›绿化、压缩、噪点和虚拟水印,自由调节做旧程度。
图片压缩
使用 ›选择画质和输出格式,预览压缩前后的大小。