I didn't learn working with classes in C++, focusing on non object oriented programming.

So I ran into a situation where I need to make sure I can serialize vectors and other primitive data types, so I can revive the variables and their value with the boost libraries, I tried the post someone did the other day and tried to compile his code on my machine however it keep saying undefined reference (while naming multiple folders) I linked the boost libraries in visual studio fine .

[text](enter image description here) enter image description here

I tried to link the boost libraries , before they weren't recognized in my visual studio code but after some configurations they are well reconized now and no red undeline appear under them which is a good thing.

Here are my launch.json configurations:

{
    "configurations": [
        
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }

    ]
    
}


For my settings.json
{
  "C_Cpp_Runner.msvcBatchPath": "",
  "C_Cpp_Runner.cCompilerPath": "C:/msys64/mingw64/bin/gcc.exe",
  "C_Cpp_Runner.cppCompilerPath": "C:/msys64/mingw64/bin/g++.exe",
  "C_Cpp_Runner.debuggerPath": "gdb",
  "C_Cpp_Runner.cStandard": "",
  "C_Cpp_Runner.cppStandard": "",
  "C_Cpp_Runner.useMsvc": false,
  "C_Cpp_Runner.warnings": [
    "-Wall",
    "-Wextra",
    "-Wpedantic",
    "-Wshadow",
    "-Wformat=2",
    "-Wconversion",
    "-Wnull-dereference",
    "-Wsign-conversion"
  ],
  "C_Cpp_Runner.enableWarnings": true,
  "C_Cpp_Runner.warningsAsError": false,
  "C_Cpp_Runner.compilerArgs": [],
  "C_Cpp_Runner.linkerArgs": [
    "\"-Lboost\""
  ],
  "C_Cpp_Runner.includePaths": [
    "C:\\msys64\\mingw64\\include\\boost"
  ],
  "C_Cpp_Runner.includeSearch": [
    "*",
    "**/*"
  ],
  "C_Cpp_Runner.excludeSearch": [
    "**/build",
    "**/build/**",
    "**/.*",
    "**/.*/**",
    "**/.vscode",
    "**/.vscode/**"
  ],
  "C_Cpp_Runner.useAddressSanitizer": false
}

for my tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:/msys64/mingw64/bin/g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-Lboost"
            ],
            "options": {
                "cwd": "C:/msys64/mingw64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}
1

There are 1 best solutions below

8
On

Boost Serialization is 100% object oriented, so you should probably not be using it.

Besides, std::vector instantiations are not primitive types.

Why not write your own routine? The following is 100% procedural and gluten-free:

Live On Coliru

#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>

std::string serialize(std::vector<int> v) {
    std::ostringstream oss;
    oss << v.size() << "\n";
    for (auto const& el : v)
        oss << " " << el;
    oss << std::endl;
    return oss.str();
}

std::string serialize(std::vector<std::string> v) {
    std::ostringstream oss;
    oss << v.size() << "\n";
    for (auto const& el : v)
        oss << " " << quoted(el);
    oss << std::endl;
    return oss.str();
}

bool deserialize(std::istream& is, std::vector<int>& into) {
    size_t n;
    for (is >> n; is && n; --n) {
        into.emplace_back();
        is >> into.back();
    }
    return is.good() || is.eof();
}

bool deserialize(std::istream& is, std::vector<std::string>& into) {
    size_t n;
    for (is >> n; is && n; --n) {
        into.emplace_back();
        is >> quoted(into.back());
    }
    return is.good() || is.eof();
}

int main() {
    // save two vectors of different sizes and types into one string
    // (you could write that to a file)
    std::string text = serialize({1, 2, 3, 4}) + serialize({"one", "two deux", "hello \"world\"!"});

    // read it back
    std::vector<int> ints;
    std::vector<std::string> strings;
    {
        std::istringstream is(text);
        bool ok = deserialize(is, ints) && deserialize(is, strings);
        std::cout << "Result: " << std::boolalpha << ok << "\n";
    }

    std::cout << "ints: (" << ints.size() << ") ";
    for (auto i : ints)
        std::cout << " " << i;
    std::cout << "\nstrings: (" << strings.size() << ") ";
    for (auto s : strings)
        std::cout << " " << quoted(s);
}

Prints the expectable

Result: true
ints: (4)  1 2 3 4
strings: (3)  "one" "two deux" "hello \"world\"!"

BONUS

Regarding references to undefined symbols: What is an undefined reference/unresolved external symbol error and how do I fix it?