在 boost 中从 graphviz 读取的问题

Problems reading from graphviz in boost

我正在尝试使用 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 进行编译,但仍然出现错误。我也曾尝试包含 <libs/graph/src/read_graphviz_new.cpp> 但后来我的程序中断了,因为它不知道 <libs/graph/src/read_graphviz_new.cpp> 是什么。我不知道下一步需要尝试什么,或者这可能不是打印出来的正确方法。任何帮助将不胜感激!

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

read_graphviz 实现在库部分(已编译),因此您需要 link 以某种方式实现。

您可以直接编译并 link 相关的 cpp 文件,甚至可以将其包含到您的文件中:

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

"canonical" 方法是编译库和 link 例如

g++ test.cpp -lboost_graph 

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

特为Win/MSVC用户补充:boost::graph头文件中没有config.h,所以本库没有实现自动链接(原因在boost.org 的第一个 BGL 文档页面:它是关于纯头文件库的,除了支持 export/import 函数的边格式,包括 grapViz 函数)。

所以,libbost-graph-*.lib 在适当的版本中应该手动链接。但是这个库又依赖于 boost-regex-*.lib。这个有自动链接,所以将 #include <boost/regex.hpp> 放在代码中的任何位置就足够了,即使没有实际使用正则表达式也是如此。此保证会自动选择合适的 boost-regex-*.lib 版本。