【问题标题】:Including read_graphviz_new.cpp from boost 1.73.0 Does Not Compile包括来自 boost 1.73.0 的 read_graphviz_new.cpp 不编译
【发布时间】:2020-09-04 12:41:47
【问题描述】:

我刚刚下载了当前最新版本的 boost (1.73.0),构建它,然后将 headers 和 libs 文件夹复制到 /usr/local/include。此代码无法编译,并在重新定义和未使用函数时出现大量错误。

#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;
}

我将编辑此问题并添加所需的任何详细信息。

详情: 我正在使用 GCC/G++ 10。我的完整应用程序确实使用了 read_graphviz、write_graphviz 和其他一些 BGL 东西,并且得到了与上面编辑的相同的错误。

编辑 我刚刚发现将 boost regex 链接到我上面的最小示例可以使其工作。我现在正在努力在最小示例中复制该问题。

【问题讨论】:

  • 这里似乎出了很多问题。包含.cpp 文件通常是错误的,并且您的包含路径看起来确实错误。您是否按照 boost::graph 文档中的示例进行操作?
  • @user14717 这样做很常见(参见例如stackoverflow.com/questions/29496182/…)。不过比较典型的是链接到Boost Graph的预建共享库
  • @larkwiot 消息是什么?你用什么编译器?此外,没有使用的功能很有意义,因为你......不使用任何东西。这就是直接包含实现文件的效果。
  • 我没有把我的包含路径放在帖子里,你怎么知道他们错了?文档说我必须动态链接它,但我看到了类似 stackoverflow.com/questions/29496182/… 的其他一些帖子,这些 cmets 暗示这个较新的文件允许静态链接 BGL。
  • @sehe 我希望你能回答;我正在使用 G++-10。这只是一个小例子,我的完整应用程序使用了这个文件中的函数,但仍然得到完全相同的错误。

标签: c++ boost boost-graph


【解决方案1】:

虽然它可能对其他人没有多大帮助,但我的问题是 #ifndef 中的一个错字,最终导致相同的标题都被包含两次,并且由于某种原因,这个标题无法接受并导致重新定义错误。我仍然收到有关#pragma 消息用法的注释,但它可以编译。

【讨论】:

    猜你喜欢
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多