2

I have just downloaded the current latest version of boost (1.73.0), built it, and then copied the headers and libs folders to /usr/local/include. This code does not compile, and spits out tons of errors on redefinitions and functions not being used.

#include <string>
#include <vector>
#include <libs/graph/src/read_graphviz_new.cpp>

struct Vertex {
    std::string name;
    std::string label;
    std::string shape;
};

struct Edge {
    std::string label;
};

struct Graph {
    std::string name;
    boost::dynamic_properties props;
};

// https://www.boost.org/doc/libs/1_73_0/libs/graph/doc/using_adjacency_list.html#sec:choosing-graph-type
typedef boost::adjacency_list<
    boost::vecS,
    boost::vecS,
    boost::directedS,

    Vertex,
    Edge,
    Graph
> graph_t;

// pulled from http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html
std::string read_file_to_string(std::string fileName) {
    std::ifstream in(fileName, std::ios::in | std::ios::binary);

    if (!in) {
        printf("\n[!] Could not open file %s!\n", fileName.c_str());
        throw(errno);
    }

    std::string contents;
    in.seekg(0, std::ios::end);
    contents.resize(in.tellg());

    in.seekg(0, std::ios::beg);
    in.read(&contents[0], contents.size());

    in.close();
    return contents;
}

using namespace std;

int main() {
    string fileName = "test.dot";
    graph_t graph(0);
    bool ret = -1;

    // https://stackoverflow.com/questions/29898195/boostread-graphviz-how-to-read-out-properties
    // https://stackoverflow.com/questions/29496182/read-graphviz-in-boostgraph-pass-to-constructor/29501850#29501850
    boost::dynamic_properties dp(boost::ignore_other_properties);
    dp.property("node_id", boost::get(&Vertex::name, graph));
    dp.property("label", boost::get(&Vertex::label, graph));
    dp.property("shape", boost::get(&Vertex::shape, graph));
    dp.property("label", boost::get(&Edge::label, graph));

    string contents = read_file_to_string(fileName);
    istringstream stream(contents);

    // https://www.boost.org/doc/libs/1_73_0/libs/graph/doc/read_graphviz.html
    ret = boost::read_graphviz(stream, graph, dp, "node_id");
    if (!ret) {
        printf("[!] Error reading graph!\n");
    }

    graph[boost::graph_bundle].name = fileName;
    graph[boost::graph_bundle].props = dp;

    return 0;
}

I will edit this question and add any details asked for.

Details: I am using GCC/G++ 10. My full app does use read_graphviz, write_graphviz, and some other BGL stuff and gets the same errors as the edited above.

EDIT I have just found that linking boost regex to my minimal example above makes it work. I am working on replicating the issue in the minimal example now.

larkwiot
  • 63
  • 6
  • Seems like a lot is going wrong here. It's generally wrong to include a `.cpp` file, and your include paths look really wrong. Did you follow an example from the `boost::graph` documentation? – user14717 May 18 '20 at 17:05
  • @user14717 It's pretty common to do this (see e.g. https://stackoverflow.com/questions/29496182/read-graphviz-in-boostgraph-pass-to-constructor/29501850#29501850). However, the more typical thing is to link to the pre-built shared library for Boost Graph – sehe May 18 '20 at 17:25
  • @larkwiot what is the message? What compiler do you use? Besides, the functions not being used makes a lot of sense since you ... don't use anything. That's the effect of including an implementation file directly. – sehe May 18 '20 at 17:25
  • I did not put my include paths in the post, how do you know they are wrong? The documentation says I have to link it dynamically, but I saw some other posts like this https://stackoverflow.com/questions/29496182/read-graphviz-in-boostgraph-pass-to-constructor/29501850#29501850 and those comments imply that this newer file allows BGL to be statically linked. – larkwiot May 18 '20 at 17:57
  • @sehe I was hoping you'd answer; I am using G++-10. This is just a minimal example, my full app uses functions from this file and still gets the exact same errors. – larkwiot May 18 '20 at 17:59
  • I realized the errors were not the exact same in the previous as they are in my app. I edited my above code to make the same errors as in my full app (minus some function not-used ones). – larkwiot May 18 '20 at 18:05
  • Ah, yeah Boost Regex is a transitive dependency of Boost Graph shared lib. If that was all, please self-wasnwer accordingly :) – sehe May 18 '20 at 18:19
  • Unrelated: why would you initialize a bool to -1? That's rubbish and will just compile to `bool ret = (-1) != 0;`. Seems a lot clearer to just `bool ret = true;`.Also, it's good style include all explicit headers required (`#include ` e.g.) – sehe May 18 '20 at 18:21
  • My own tests with the code and boost 1.73.0 indicate that indeed there is no compilation error. Leaving just the linker error that you already discovered. #caseclosed? – sehe May 18 '20 at 18:24

1 Answers1

1

Though it may not help other people much, my issue was a typo in an #ifndef that ultimately resulted in the same headers all getting included twice, and for some reason this one was the one that couldn't take it and caused the redefinition bugs. I do still get a note about #pragma message usage, but it compiles.

larkwiot
  • 63
  • 6