c++ - g++ 中的链接问题

标签 c++ boost

我正在使用 C++ boost 库编写代码,但遇到链接错误 -

代码

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

链接错误 -

 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

我不明白为什么会出现 undefined reference 的错误。

请帮助我解决这个问题。

提前致谢。

最佳答案

您是否从某处复制/粘贴了目标文件或库文件?它们必须针对相同的二进制接口(interface)构建。

这看起来很像您使用的是 C++11 header ,但使用的是 C++03 运行时,因为您编译的代码期望找到采用 std::string 的 ifstream 构造函数重载(在 C++11 中引入),但链接器找不到此类重载的实现证据。

使用相同的工具链重建所有内容,包括 Boost 库,看看会发生什么。

关于c++ - g++ 中的链接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32115378/

相关文章:

c++ - 为什么我必须将这些引用参数声明为 const 或按值传递?

c++ - 内联结构声明的大小

c++ - 在套接字关闭之前不发送数据

带有 boost scoped_ptr 的 C++ 多态性

c++ - SWIG Lua 和传递数组

c++ - 使用点积计算两个 vector 之间的角度

c++ - Boost 多索引容器与基于 std::unordered_map( map 的 map )的多级映射容器

c++ - boost ssl 连接过程失败

c++ - Boost Graph Library - 仅通过连接的顶点进行深度优先搜索

c++ - 如何制作一个 set<string> 支持通过 C 字符串进行查找,而无需 std::string 中介?