Sailanarmo
Sailanarmo

Reputation: 1181

Problems reading from graphviz in boost

I am trying to read in the following graph using boost's 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"];
}

However, every time I try to compile it I get a nasty error:

/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

I have no idea what any of it means, I have tried compiling with g++ -lboost_graph test.cpp and I still get the error. I have also tried to include <libs/graph/src/read_graphviz_new.cpp> but then my program breaks because it does not know what <libs/graph/src/read_graphviz_new.cpp> is. I don't know what I need to try next, or maybe this isn't the correct way to print this out. Any help would be greatly appreciated!

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

Upvotes: 1

Views: 453

Answers (2)

Supplement especially for Win/MSVC users: there is no config.h among the boost::graph headers, so the automatic linkage for this library isn't implemented (the reason is pointed out at the very first BGL doc page at boost.org: it's about pure header library, except the side format supporting export/import functions, including grapViz ones).

So, libbost-graph-*.lib in the appropriate version should be linked manually. But this library in turn has a dependency from boost-regex-*.lib. This one has automatic linkage, so it's enought to put #include <boost/regex.hpp> anywhere in your code, even if there is no actual usage of regexps. This warranty that appropriate version of boost-regex-*.lib would be choosen automatically.

Upvotes: 0

sehe
sehe

Reputation: 392893

The read_graphviz implementation is in the library part (compiled) so you need to link that some way.

You could directly compile and link the relevant cpp file, or even include it into yours:

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

The "canonical" way is to compile the library and link e.g. with

g++ test.cpp -lboost_graph 

And yes, order matters! (Why does the order of '-l' option in gcc matter?)

Upvotes: 1

Related Questions