【问题标题】:Problems reading from graphviz in boost在 boost 中从 graphviz 读取的问题
【发布时间】:2018-05-10 23:08:33
【问题描述】:

我正在尝试使用 boost 的 graphviz 阅读下图:

graph G {
0[label="1"];
1[label="0"];
2[label="1"];
3[label="1"];
0--1 [label="1.2857"];
1--2 [label="4.86712"];
1--3 [label="2.29344"];
}

但是,每次我尝试编译它时都会遇到一个严重的错误:

/tmp/ccnZnPad.o: In function "bool boost::read_graphviz_new<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, Vertex, Edge, boost::no_property, boost::listS> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, Vertex, Edge, boost::no_property, boost::listS>&, boost::dynamic_properties&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):test.cpp(.text._ZN5boost17read_graphviz_newINS_14adjacency_listINS_4vecSES2_NS_11undirectedSE6Vertex4EdgeNS_11no_propertyENS_5listSEEEEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERT_RNS_18dynamic_propertiesESG_[_ZN5boost17read_graphviz_newINS_14adjacency_listINS_4vecSES2_NS_11undirectedSE6Vertex4EdgeNS_11no_propertyENS_5listSEEEEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERT_RNS_18dynamic_propertiesESG_]+0x98): undefined reference to boost::detail::graph::read_graphviz_new(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::detail::graph::mutate_graph*)' collect2: error: ld returned 1 exit status

我不知道它是什么意思,我试过用g++ -lboost_graph test.cpp 编译,但我仍然得到错误。我也尝试包含&lt;libs/graph/src/read_graphviz_new.cpp&gt;,但后来我的程序中断了,因为它不知道&lt;libs/graph/src/read_graphviz_new.cpp&gt; 是什么。我不知道接下来我需要尝试什么,或者这可能不是打印出来的正确方法。任何帮助将不胜感激!

#include <fstream>

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
//#include <libs/graph/src/read_graphviz_new.cpp> //breaks if I try to include
#include <boost/graph/graph_utility.hpp>

struct Vertex 
{
  bool isLeaf;
};

struct Edge
{
  double weight;
};

typedef boost::adjacency_list<boost::vecS,boost::vecS, boost::undirectedS, Vertex, Edge> Graph;

int main()
{
    Graph g;
    boost::dynamic_properties dp;
    dp.property("label", get(&Vertex::isLeaf, g));
    dp.property("label", get(&Edge::weight, g));

    std::ifstream dot("baseTree.dot");

    boost::read_graphviz(dot,g,dp);
    write_graphviz_dp(std::cout, g, dp); 
}

【问题讨论】:

  • g++ -lboost_graph test.cpp 是整个编译行吗?请注意,通常库应该在之后需要来自它们的符号的目标文件传递。
  • @VTT 是的,这是整个编译行。
  • @VTT 所以我需要编译成g++ test.cpp -lboost_graph ?

标签: c++ boost graph graphviz


【解决方案1】:

read_graphviz 实现位于库部分(已编译),因此您需要以某种方式链接它。

您可以直接编译和链接相关的cpp文件,甚至可以将其包含在您的文件中:

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

“规范”的方式是编译库和链接,例如与

g++ test.cpp -lboost_graph 

是的,顺序很重要! (Why does the order of '-l' option in gcc matter?)

【讨论】:

    【解决方案2】:

    特别针对 Win/MSVC 用户的补充:boost::graph 标头中没有 config.h,因此没有实现此库的自动链接(原因在第一个 BGL 文档页面中指出boost.org:它是关于纯头库的,除了支持导出/导入功能的侧面格式,包括 grapViz 的)。

    因此,应手动链接相应版本中的libbost-graph-*.lib。但是这个库又依赖于boost-regex-*.lib。这个具有自动链接功能,因此将#include &lt;boost/regex.hpp&gt; 放在代码中的任何位置就足够了,即使没有实际使用正则表达式。此保证将自动选择适当版本的boost-regex-*.lib

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 2012-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多