2

I am writing code using C++ boost library, I am getting a linking error -

Code

#include "test/support/checkers/log-entry-presence-checkers.h"
#include <sstream>
#include <iostream>
#include <fstream>
#include <boost/foreach.hpp>   
#include <boost/test/unit_test.hpp>

using namespace std;


namespace YumaTest
{
LogEntryPresenceChecker::LogEntryPresenceChecker(
    const vector<string>& expPresentText )
: checkStrings_( expPresentText )
{
}
LogEntryPresenceChecker::~LogEntryPresenceChecker()
{
}
void LogEntryPresenceChecker::operator()( const string& logFilePath ) const
{
     ifstream logFile;
     string line;
bool entryFound;

BOOST_FOREACH ( const string& val, checkStrings_ )
{
    entryFound = false;
    BOOST_TEST_MESSAGE( "\tChecking " << val << " is present" );
    logFile.open( logFilePath );
    if (logFile.is_open()) {
         while ( logFile.good() && entryFound == false ) {
            getline(logFile, line);
            if ( line.find( val ) != string::npos ) {
                entryFound = true;
            }
        }
        logFile.close();
    }
    BOOST_CHECK_EQUAL( true, entryFound );
}
}

Linking Error -

 g++ output/b64.o output/bobhash.o output/cap.o output/cfg.o output/cli.o output/conf.o output/def_reg.o output/dlq.o output/ext.o output/grp.o output/help.o output/json_wr.o output/log.o output/ncx_appinfo.o output/ncx.o output/ncx_feature.o output/ncx_list.o output/ncxmod.o output/ncx_num.o output/ncx_str.o output/obj.o output/obj_help.o output/op.o output/plock.o output/plock_cb.o output/rpc.o output/rpc_err.o output/runstack.o output/ses.o output/ses_msg.o output/status.o output/tk.o output/top.o output/tstamp.o output/typ.o output/val.o output/val_util.o output/var.o output/xml_msg.o output/xmlns.o output/xml_util.o output/xml_val.o output/xml_wr.o output/xpath1.o output/xpath.o output/xpath_wr.o output/xpath_yang.o output/yang.o output/yang_ext.o output/yang_grp.o output/yang_obj.o output/yang_parse.o output/yang_typ.o output/yin.o output/yinyang.o output/send_buff.o output/agt_acm.o output/agt.o output/agt_cap.o output/agt_cb.o output/agt_cfg.o output/agt_cli.o output/agt_commit_complete.o output/agt_connect.o output/agt_hello.o output/agt_if.o output/agt_ncx.o output/agt_not.o output/agt_plock.o output/agt_proc.o output/agt_rpc.o output/agt_rpcerr.o output/agt_ses.o output/agt_signal.o output/agt_state.o output/agt_sys.o output/agt_time_filter.o output/agt_timer.o output/agt_top.o output/agt_tree.o output/agt_util.o output/agt_val.o output/agt_val_parse.o output/agt_xml.o output/agt_xpath.o output/agt_yuma_arp.o output/agt_ncxserver.o output/checker-group.o output/string-presence-checkers.o output/log-entry-presence-checkers.o output/sil-callback-log.o output/callback-checker.o output/sil-callback-controller.o output/device-test-db.o output/device-test-db-debug.o output/state-data-test-db.o output/abstract-nc-session.o output/nc-base-query-test-engine.o output/nc-default-operation-config.o output/nc-query-test-engine.o output/nc-query-utils.o output/nc-strings.o output/yuma-op-policies.o output/abstract-global-fixture.o output/test-context.o output/base-suite-fixture.o output/query-suite-fixture.o output/simple-container-module-fixture.o output/simple-yang-fixture.o output/device-module-fixture.o output/device-get-module-fixture.o output/device-module-common-fixture.o output/state-get-module-fixture.o output/state-data-module-common-fixture.o output/cpp-unit-op-formatter.o output/log-utils.o output/ptree-utils.o output/base64.o output/NCMessageBuilder.o output/xpo-query-builder.o output/state-data-query-builder.o output/spoof-nc-session.o output/spoof-nc-session-factory.o output/running-cb-checker.o output/candidate-cb-checker.o output/integ-cb-checker-factory.o output/integ-fixture-helper.o output/integ-fixture-helper-factory.o output/simple-edit-tests.o output/default-none-tests.o output/simple-edit-running.o -Wall -g -lxml2 -lboost_unit_test_framework -Wl,--export-dynamic -o test-simple-edit-running
output/log-entry-presence-checkers.o: In function `YumaTest::LogEntryPresenceChecker::operator()(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const':
/home/abhishek/NETCONF_8_3_0/code/opensource/openyuma/netconf/test/integ-tests/../../test/support/checkers/log-entry-presence-checkers.cpp:52: undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::open(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::_Ios_Openmode)'
output/log-entry-presence-checkers.o: In function `YumaTest::LogEntryNonPresenceChecker::operator()(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const':
/home/abhishek/NETCONF_8_3_0/code/opensource/openyuma/netconf/test/integ-tests/../../test/support/checkers/log-entry-presence-checkers.cpp:89: undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::open(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::_Ios_Openmode)'
output/abstract-nc-session.o: In function `YumaTest::AbstractNCSession::concatenateLogFiles()':
/home/abhishek/NETCONF_8_3_0/code/opensource/openyuma/netconf/test/integ-tests/../../test/support/nc-session/abstract-nc-session.cpp:66: undefined reference to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::_Ios_Openmode)'
output/abstract-nc-session.o: In function `YumaTest::AbstractNCSession::appendLog(std::basic_ofstream<char, std::char_traits<char> >&, unsigned short)':
/home/abhishek/NETCONF_8_3_0/code/opensource/openyuma/netconf/test/integ-tests/../../test/support/nc-session/abstract-nc-session.cpp:84: undefined reference to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::_Ios_Openmode)'
collect2: ld returned 1 exit status

I am not getting why there is a error of undefined reference.

Please help me out How can I get rid of this issue.

Thanks in Advance.

user2798118
  • 385
  • 1
  • 2
  • 16
  • @sehe Not convinced that's helpful in this instance. Did you read the question? – Lightness Races in Orbit Aug 20 '15 at 10:13
  • Mmm. Maybe not close enough. "I am not getting why there is a error of undefined reference. Please help me out How can I get rid of this issue." made me decide. I agree it's funny business here – sehe Aug 20 '15 at 10:15
  • @sehe I think that FAQ is good for the usual flurry of generic link failures, but this one is potentially more nuanced. If nothing else, I cannot see a relevant answer there, though I skimmed. – Lightness Races in Orbit Aug 20 '15 at 10:27
  • You didn't hear me say otherwise :) Guess who upvoted – sehe Aug 20 '15 at 10:28
  • 1
    Are you using the exact same `g++` to link as was used to compile those object files? – Jonathan Wakely Aug 20 '15 at 10:39
  • 3
    Try reducing the amount of code? Does something like `int main() {std::ifstream(std::string("hehe"));}` work? [mcve] – anatolyg Aug 20 '15 at 10:41
  • 3
    Yes, you should be able to reproduce the problem with simply: `#include #include int main() { std::ifstream f(std::string("f")); }` – Jonathan Wakely Aug 20 '15 at 10:42
  • 1
    @Jonathan Error when compiling the program mentioned by you - `error: no matching function for call to âstd::basic_fstream >::basic_fstream(std::string)â /usr/lib/gcc/i586-redhat-linux/4.4.1/../../../../include/c++/4.4.1/fstream:795: note: candidates are: std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits]` – user2798118 Aug 20 '15 at 10:52
  • 1
    @user2798118: Yep, looks like the same error. So there's your minimal testcase (see how debugging is critical?). Nothing to do with Boost it turns out. – Lightness Races in Orbit Aug 20 '15 at 10:54
  • Did you compile with -std=c++11? What GCC versions are you using? That constructor requires C++11 support. Your original error suggests that the `.o` files were compiled by a compiler with C++11 support, but the version you are using to link doesn't have it ... which is not supported, you must not link with an older G++ than you compiled with, ever. The error on the minimal example means either you didn't use `-std=c++11` or confirms that you're using an older GCC than was used to compile the `.o` files. – Jonathan Wakely Aug 20 '15 at 11:02
  • @Jonathan Wakely gcc-4.4.1 on fedora11 – user2798118 Aug 20 '15 at 11:05
  • Facepalm. Are you a time traveller? That version of Fedora has been unsupported for many many years. Still, GCC 4.4.1 **does** support those constructors, so you shouldn't be getting that linker error. (The error on the minimal example is because you didn't use `-std=c++0x` to compile it). – Jonathan Wakely Aug 20 '15 at 11:05
  • @Jonathan Wakely Actually our product is currently using fedora 11 :( – user2798118 Aug 20 '15 at 11:09
  • What do you get if you compile the minimal example with `-std=c++0x` ? – Jonathan Wakely Aug 20 '15 at 11:11
  • @Jonathan Wakely `g++ -std=c++0x a.cpp /tmp/cc27UnF0.o: In function `main': a.cpp:(.text+0x6c): undefined reference to `std::basic_fstream >::basic_fstream(std::basic_string, std::allocator > const&, std::_Ios_Openmode)' collect2: ld returned 1 exit status ` – user2798118 Aug 20 '15 at 11:14
  • @ Jonathan Wakely Installed gcc version `rpm -qa | grep gcc libgcc-4.4.1-2.fc11.i586 gcc-4.4.1-2.fc11.i586 gcc-c++-4.4.1-2.fc11.i586` – user2798118 Aug 20 '15 at 11:17
  • `rpm -q libstdc++` ? – Jonathan Wakely Aug 20 '15 at 11:30
  • `libstdc++-4.4.1-2.fc11.i586` – user2798118 Aug 20 '15 at 11:33
  • Very strange, looks like a bug in the RPM packages, which will never get fixed because you're on a dead version of Fedora. Bad luck. – Jonathan Wakely Aug 20 '15 at 11:34
  • I tried on Fedora 18, it is working fine. Thanks @Jonathan Wakely – user2798118 Aug 20 '15 at 12:00
  • Fedora 18 is also unsupported. If you want to work on old systems why not use something stable but still supported like CentOS? That has GCC 4.4.7 and the example compiles and links without problems. – Jonathan Wakely Aug 20 '15 at 13:52

1 Answers1

2

Did you copy/paste the object files or library files from somewhere? They must be built against the same binary interface.

This looks very much like you're using C++11 headers but a C++03 runtime, because your compiled code expects to find ifstream constructor overloads taking std::string (introduced in C++11), but the linker can find no evidence of implementation for such overloads.

Rebuild everything, including your Boost library, with the same toolchain and see what happens.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • All object are created during compilation, After compilation I am trying to link them. I tried to link along with **-std=c++11** also . Still I am getting the same error. – user2798118 Aug 20 '15 at 10:26
  • @user2798118: Yes I know what compilation is – Lightness Races in Orbit Aug 20 '15 at 10:27
  • `-std=c++11` is ignored during linking, there is only one runtime library, which works for both C++03 and C++11. The only way to get unresolved references to those C++11 constructors is to compile with one version of G++ and use an older version to link. – Jonathan Wakely Aug 20 '15 at 10:41
  • @JonathanWakely: Okay thanks. That's what I was trying to get at regarding the runtime library (libs being compiled with an older GCC) - the `-std=c++11` was admittedly a bit silly though I did at least say it would be a hacky workaround if it worked :P – Lightness Races in Orbit Aug 20 '15 at 10:43