1

Given this code:

#include <iostream>
#include <fstream>
#include <boost/version.hpp> //106501 / "1_65_1"
#include <boost/graph/graph_utility.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphml.hpp>
#include <boost/graph/graphviz.hpp>
#include "/usr/local/boost_1_65_1/libs/graph/src/read_graphviz_new.cpp"
#include "BoostGraph.hpp"

using namespace std;
using namespace boost;

int main()
{
    string dot_file = "init_topology_0.dot";

    std::ifstream inFile;
    inFile.open(dot_file, std::ifstream::in);

    typedef boost::adjacency_list<> Graph;
    Graph g;
    boost::dynamic_properties dp;

    boost::read_graphviz(inFile, g, dp);
}

Compiled with CMAKE:

cmake_minimum_required(VERSION 3.1)
project(boostGraph)

set(BOOST_ROOT "/usr/local/boost_1_65_1")
set(BOOST_INCLUDE_DIRS "${BOOST_ROOT}/boost")
set(BOOST_LIBRARY_DIRS "${BOOST_ROOT}/stage/lib")

find_package(Boost REQUIRED regex date_time system filesystem thread graph program_options)
include_directories(${BOOST_INCLUDE_DIRS})
link_directories(boostGraph ${BOOST_LIBRARY_DIRS})

add_executable(boostGraph main.cpp)

target_compile_options(boostGraph PUBLIC -I /usr/lib/boost/include -L /usr/lib/boost/lib -lboost_graph -lboost_regex -lboost_regex-mt -lboost_filesystem -lboost_system)

It gives the error:

CMakeFiles/boostGraph.dir/main.cpp.o: In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
main.cpp:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j]+0x2a): undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'
CMakeFiles/boostGraph.dir/main.cpp.o: In function `bool boost::regex_search<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)':
main.cpp:(.text._ZN5boost12regex_searchIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISB_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SJ_RNS_13match_resultsISJ_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsESJ_[_ZN5boost12regex_searchIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISB_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SJ_RNS_13match_resultsISJ_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsESJ_]+0xba): undefined reference to `boost::re_detail_106501::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::find()'
CMakeFiles/boostGraph.dir/main.cpp.o: In function `boost::re_detail_106501::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::perl_matcher(__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >)':
main.cpp:(.text._ZN5boost16re_detail_10650112perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISC_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC2ESC_SC_RNS_13match_resultsISC_SF_EERKNS_11basic_regexIcSJ_EENS_15regex_constants12_match_flagsESC_[_ZN5boost16re_detail_10650112perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESaINS_9sub_matchISC_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC5ESC_SC_RNS_13match_resultsISC_SF_EERKNS_11basic_regexIcSJ_EENS_15regex_constants12_match_flagsESC_]+0x110): undefined reference to `boost::re_detail_106501::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)'
collect2: error: ld returned 1 exit status
CMakeFiles/boostGraph.dir/build.make:120: recipe for target 'boostGraph' failed
make[2]: *** [boostGraph] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/boostGraph.dir/all' failed
make[1]: *** [CMakeFiles/boostGraph.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

Since "/usr/local/boost_1_65_1/libs/graph/src/read_graphviz_new.cpp" had to be included manually for boost::read_graphviz to be compiled and since both read_graphviz and boost_regex are precompiled libraries and not header only, I suspected linking issue.
But after adding linking directions to cmake as since above, cmake do find the regex library as can be seen in this output:

user@user:~/CLionProjects/hGraph/build$ cmake ..
-- Boost version: 1.65.1
-- Found the following Boost libraries:
--   regex
--   date_time
--   system
--   filesystem
--   thread
--   graph
--   program_options
--   chrono
--   atomic
-- Configuring done
-- Generating done
-- Build files have been written to: /home/john/CLionProjects/hydronGraph/build

So what might be the problem?

Update: Still not sure what was the exact problem, but the following CMAKE file do compile the code:

cmake_minimum_required(VERSION 3.1)
project(boostGraph)
find_package(Boost REQUIRED COMPONENTS regex date_time system filesystem thread graph program_options)
add_executable(boostGraph main.cpp)
target_link_libraries(boostGraph Boost::regex Boost::date_time Boost::system Boost::filesystem Boost::thread Boost::graph Boost::program_options)

This is relevant since there are similar issues asked online and I could not find this solution in any of them. This syntax does not apear in any documentations I could find.

credit: François Kooman found this solution me. see code on his github

sivan shani
  • 87
  • 1
  • 8
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ulrich Eckhardt May 06 '20 at 06:06
  • @UlrichEckhardt thanks for the reply. perhaps the part about "Different versions of libraries" from the link you offered is relevant here, I am not sure. I do not think however that it is possible to deduce from it what is actually the issue I had. – sivan shani May 06 '20 at 10:58
  • I had exactly the same problem. I had linking problems using the old fashioned build tools that I was used to using. Cmake worked well. Not sure why, as I'm unfamiliar with Cmake, but it looks like I'm going to get familiar with it. Thanks! – Edward Doolittle Dec 06 '20 at 00:49
  • 1
    Just to follow up on this ... Using cmake solved my problem on Ubuntu. On MacOS with homebrew, I still had a linking problem even with cmake. I rebuilt the boost libraries from scratch and tried again, and noticed that I had a problem with cmake and g++-10 that came with homebrew, but no problem with cmake and clang. I believe the issue may boil down to a mismatch between the tool chain used to build boost and the tool chain used to build the application. Cmake works sometimes because it may choose a different toolchain than compiling "by hand". – Edward Doolittle Dec 06 '20 at 04:35
  • had issues on windows: solved it setting up cmake like this: set (BOOST_INCLUDE_DIRS D:/vcpkg/installed/x64-windows/include) find_package(Boost 1.77.0 REQUIRED COMPONENTS regex graph) include_directories(${BOOST_INCLUDE_DIRS}) add_executable(graph_test main.cpp ) target_link_libraries(graph_test ${Boost_LIBRARIES}) – Pj Toopmuch Apr 12 '22 at 18:06

0 Answers0