0

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"
}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • 2
    The first thing to do when asking a Question is figure out what the question is that you want to ask; the second thing to do is include that question in your Question. – Jeremy Friesner Jun 15 '23 at 01:06
  • 2
    Please provide, as text in the question itself, not in images, a complete [mre] for your issue, including the full compiler output it produces. See [ask]. – user17732522 Jun 15 '23 at 01:18
  • 1
    That said, `-Lboost` is certainly wrong. Libraries are provided with the lower-case `-l` option, not upper-case `-L`, and boost is (typically?) split into multiple libraries, not a single one called `(lib)boost.(so|dll)`. – user17732522 Jun 15 '23 at 01:22
  • 1
    *I have an urgent question* - [Under what circumstances may I add "urgent" or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/questions/326569/under-what-circumstances-may-i-add-urgent-or-other-similar-phrases-to-my-quest) | [Why should I not upload images of code/data/errors?](https://meta.stackoverflow.com/q/285551) – Evg Jun 15 '23 at 01:29
  • Thanks for helping me. So how do I write simple program using boost libraries to serialize primitive data types without using classes. Please provide an example. – user22075622 Jun 15 '23 at 01:37
  • When I changed to -lboost it now displays this error : Starting build... C:/msys64/mingw64/bin/g++.exe -fdiagnostics-color=always -g "C:\Users\doukm\Documents\CPPrograms\Aissata toure\sterelizing.cpp" -o "C:\Users\doukm\Documents\CPPrograms\Aissata toure\sterelizing.exe" -lboost C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lboost: No such file or directory collect2.exe: error: ld returned 1 exit status Build finished with error(s). – user22075622 Jun 15 '23 at 01:42
  • @user22075622 `-L` is used to indicate library paths, so it makes perfect sense IFF your libraries are found in the directory named `boost`. Randomly changing command line to `-lboost` makes zero sense and is just a waste of time. – sehe Jun 15 '23 at 01:43
  • However when I maintain and stay with -Lboost while in other programs it compiles without any problem – user22075622 Jun 15 '23 at 01:47
  • So what about my other question, please help me, I'm worried. The question is a simple program with example on how to serialize primitives data types so I can reuse the variables even when I re-execute the program using the following three libraries from boost ? #include #include #include sorry for being a little bothersome I just need help again please! – user22075622 Jun 15 '23 at 01:54
  • Don't be sorry, just focus. You need to focus. The help is wasted otherwise. I hope my answer helps you shift from solving unnecessary complexity (using Boost Serialization when you didn't even learn how to use classes is never going to work), and back to the problem you're trying to solve: saving data and reading it back. – sehe Jun 15 '23 at 02:03
  • *"but after some configurations they are well reconized now and no red undeline appear under them which is a good thing."* -- So problem solved? It's not clear what remaining issue you need help with (possibly because I did not look at the linked images). – JaMiT Jun 15 '23 at 03:57
  • I'm still struggling at saving data and manipulating the variables and their due value after re-execution of program. An example is, in our assignment they said to arrange customers in alphabetical orders, when I register them and try arrange them it works without problem, but once I exit the program and re-execute it no longer recognize the variables and doesn't arrange customer names in Alphabetical order. I hope you understand my major issues now! Thanks in advance! – user22075622 Jun 15 '23 at 22:26
  • @user22075622 *"I hope you understand my major issues now!"* -- not really, and besides explanations in comments don't count. Treat comments as if they might disappear tomorrow. Any important information that would improve your question should be edited into the question itself. – JaMiT Jun 16 '23 at 03:15

1 Answers1

3

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?

sehe
  • 374,641
  • 47
  • 450
  • 633
  • I'm a noob, please use simple procedure with explanation. Just one more time please! I don't understand functions like "quoted" emplace back etc... Your code is too strong for beginners like me. I'm familiar with functions like push_back(), pop-back() and so on. – user22075622 Jun 15 '23 at 02:08
  • Also I'm using files to store data , the issue is, while I execute the program I am able to do all kind of operations, but once I exit and re-execute the program, I can't use for loops to for example to pick a specific element in the vector and process something with it. – user22075622 Jun 15 '23 at 02:13
  • I am well aware. It doesn't get much simpler than this, and it's a million times simpler than using boost ([I should know](https://stackoverflow.com/tags/boost-serialization/topusers)). I specifically used *only* standard library functions and they're all documented, e.g. https://en.cppreference.com/w/cpp/io (for istream and quoted etc) and e.g. https://en.cppreference.com/w/cpp/container/vector/emplace_back. – sehe Jun 15 '23 at 02:13
  • 1
    Thank You so much, May God rewards you for your help, I will try your instructions. If I have any questions I will come back, for now I'm going to try what you shared with me. Once again, thank you! – user22075622 Jun 15 '23 at 02:19
  • This is what I meant with the comment "you could write this to a file": [demo expanded to write to a file (saved.txt) and read it back on the second run](http://coliru.stacked-crooked.com/a/b6612a6d568caefc). Good luck with the assignment! – sehe Jun 15 '23 at 02:21
  • By the way C++ is my favorite language just to inform you! – user22075622 Jun 15 '23 at 02:26
  • @user22075622 I love to hear that. It's important to have fun while learning. Keep up the good work – sehe Jun 15 '23 at 02:27
  • You may as well consider me as your student from now on ! – user22075622 Jun 15 '23 at 02:31