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.
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.
Related
I am trying to read a jpg file with boost::gil. I started with the following code snippets but I got stuck already.
#include <iostream>
#include <vector>
#include <string>
#include <boost/gil/gil_all.hpp>
#include <boost/gil/extension/io/jpeg_dynamic_io.hpp>
void ReadAnImage( std::string fname ){
std::vector<std::vector<float> > points;
boost::gil::rgb8_image_t img;
boost::gil::jpeg_read_image( fname , img );
}
int main( void ){
ReadAnImage( "pic.jpg" );
}
When I try to compile this code with
g++ -I ~/programs/cpp/boost/include/ -std=c++11 main.cpp -o main
I get the following error
MainNew.cpp:(.text._ZN5boost3gil6detail11jpeg_reader4initEv[_ZN5boost3gil6detail11jpeg_reader4initEv]+0x1a): undefined reference to jpeg_std_error' MainNew.cpp:(.text._ZN5boost3gil6detail11jpeg_reader4initEv[_ZN5boost3gil6detail11jpeg_reader4initEv]+0x3f): undefined reference to jpeg_CreateDecompress'
MainNew.cpp:(.text._ZN5boost3gil6detail11jpeg_reader4initEv[_ZN5boost3gil6detail11jpeg_reader4initEv]+0x61): undefined reference to jpeg_stdio_src' MainNew.cpp:(.text._ZN5boost3gil6detail11jpeg_reader4initEv[_ZN5boost3gil6detail11jpeg_reader4initEv]+0x76): undefined reference to jpeg_read_header'
/tmp/ccrGnMRc.o: In function boost::gil::detail::jpeg_reader::~jpeg_reader()': MainNew.cpp:(.text._ZN5boost3gil6detail11jpeg_readerD2Ev[_ZN5boost3gil6detail11jpeg_readerD5Ev]+0x18): undefined reference to jpeg_destroy_decompress'
/tmp/ccrGnMRc.o: In function void boost::gil::detail::jpeg_reader::apply<boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned char, boost::gil::layout<boost::mpl::ve ctor3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t>, boost::mpl::range_c<int, 0, 3> > >*> > > >(boost::gil::image_view<boost::gil::memory_based_2d_locator<boost::gil::memory_based_step_iterator<boost::gil::pixel<unsigned cha r, boost::gil::layout<boost::mpl::vector3<boost::gil::red_t, boost::gil::green_t, boost::gil::blue_t>, boost::mpl::range_c<int, 0, 3> > >*> > > const&)': MainNew.cpp:(.text._ZN5boost3gil6detail11jpeg_reader5applyINS0_10image_viewINS0_23memory_based_2d_locatorINS0_26memory_based_step_iteratorIPNS0_5pixelIhNS0_6layoutINS_3mpl7vector3INS0_5red_tENS0_7green_tENS0_6blue_tEEENS9_7range_cIiLi0ELi 3EEEEEEEEEEEEEEEvRKT_[_ZN5boost3gil6detail11jpeg_reader5applyINS0_10image_viewINS0_23memory_based_2d_locatorINS0_26memory_based_step_iteratorIPNS0_5pixelIhNS0_6layoutINS_3mpl7vector3INS0_5red_tENS0_7green_tENS0_6blue_tEEENS9_7range_cIiLi0 ELi3EEEEEEEEEEEEEEEvRKT_]+0x2e): undefined reference to jpeg_start_decompress'
MainNew.cpp:(.text.ZN5boost3gil6detail11jpeg_reader5applyINS0_10image_viewINS0_23memory_based_2d_locatorINS0_26memory_based_step_iteratorIPNS0_5pixelIhNS0_6layoutINS_3mpl7vector3INS0_5red_tENS0_7green_tENS0_6blue_tEEENS9_7range_cIiLi0ELi
3EEEEEEEEEEEEEEEvRKT[ZN5boost3gil6detail11jpeg_reader5applyINS0_10image_viewINS0_23memory_based_2d_locatorINS0_26memory_based_step_iteratorIPNS0_5pixelIhNS0_6layoutINS_3mpl7vector3INS0_5red_tENS0_7green_tENS0_6blue_tEEENS9_7range_cIiLi0
ELi3EEEEEEEEEEEEEEEvRKT]+0x13e): undefined reference to jpeg_read_scanlines' MainNew.cpp:(.text._ZN5boost3gil6detail11jpeg_reader5applyINS0_10image_viewINS0_23memory_based_2d_locatorINS0_26memory_based_step_iteratorIPNS0_5pixelIhNS0_6layoutINS_3mpl7vector3INS0_5red_tENS0_7green_tENS0_6blue_tEEENS9_7range_cIiLi0ELi 3EEEEEEEEEEEEEEEvRKT_[_ZN5boost3gil6detail11jpeg_reader5applyINS0_10image_viewINS0_23memory_based_2d_locatorINS0_26memory_based_step_iteratorIPNS0_5pixelIhNS0_6layoutINS_3mpl7vector3INS0_5red_tENS0_7green_tENS0_6blue_tEEENS9_7range_cIiLi0 ELi3EEEEEEEEEEEEEEEvRKT_]+0x1ad): undefined reference to jpeg_finish_decompress'
collect2: error: ld returned 1 exit status
I am not quite sure what I am doing wrong. Because according to the documentation
https://www.boost.org/doc/libs/1_74_0/libs/gil/doc/html/index.html
the jpeg_read_image should be included in boost::gil. Am I maybe missing another header file??
Any hints are much appreciated.
This question already has an answer here:
Linking error using Arduino library with c++'s stringstream
(1 answer)
Closed 4 years ago.
I'm using an ESP8266 with the Arduino IDE to work on a microcontroller project. However I also want to use it as an opportunity to learn more about C++. I'm trying to deserialize a string like "key1:value1;key2:value2;..." into a map of strings, and I came across this question which seemed relevant: Parsing a comma-delimited std::string
So I put it into a function like
void SimpleKeyPairs::deserializeKeyPairs(std::string input, std::map<std::string, std::string>* output)
{
std::stringstream ss(input);
std::string key;
std::string value;
while(ss.good()) {
std::getline(ss, key, ':');
std::getline(ss, value, ';');
output->insert(std::make_pair(key, value));
}
}
However I'm getting compiler errors (copied from IDE) and unfortunately I can't figure out what I need to fix. I did isolate the problem to the line std::stringstream ss(input); but I'm not sure what the issue is.
Arduino: 1.6.13 (Mac OS X), Board: "Adafruit HUZZAH ESP8266, 80 MHz, 4M (3M SPIFFS), v2 Prebuilt (MSS=536), Disabled, None, 921600"
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o):(.literal._ZSt9use_facetISt5ctypeIcEERKT_RKSt6locale[std::ctype<char> const& std::use_facet<std::ctype<char> >(std::locale const&)]+0x4): undefined reference to `std::__throw_bad_cast()'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o): In function `std::ctype<char> const& std::use_facet<std::ctype<char> >(std::locale const&)':
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/bits/locale_classes.tcc:114: undefined reference to `std::__throw_bad_cast()'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o): In function `std::numpunct<char> const& std::use_facet<std::numpunct<char> >(std::locale const&)':
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/bits/locale_classes.tcc:114: undefined reference to `std::__throw_bad_cast()'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o): In function `std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > > const& std::use_facet<std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > > >(std::locale const&)':
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/bits/locale_classes.tcc:114: undefined reference to `std::__throw_bad_cast()'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o): In function `std::num_get<char, std::istreambuf_iterator<char, std::char_traits<char> > > const& std::use_facet<std::num_get<char, std::istreambuf_iterator<char, std::char_traits<char> > > >(std::locale const&)':
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/bits/locale_classes.tcc:114: undefined reference to `std::__throw_bad_cast()'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(locale-inst.o):/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/bits/locale_classes.tcc:114: more undefined references to `std::__throw_bad_cast()' follow
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(c++locale.o):(.literal._ZNSt6locale5facet18_S_create_c_localeERPiPKcS1_+0x4): undefined reference to `std::__throw_runtime_error(char const*)'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(c++locale.o): In function `std::locale::facet::_S_create_c_locale(int*&, char const*, int*)':
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/src/c++98/c++locale.cc:207: undefined reference to `std::__throw_runtime_error(char const*)'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(ios-inst.o):(.literal._ZNSt9basic_iosIcSt11char_traitsIcEE5clearESt12_Ios_Iostate[std::basic_ios<char, std::char_traits<char> >::clear(std::_Ios_Iostate)]+0x4): undefined reference to `std::__throw_ios_failure(char const*)'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(ios-inst.o): In function `std::basic_ios<char, std::char_traits<char> >::clear(std::_Ios_Iostate)':
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/bits/basic_ios.tcc:122: undefined reference to `std::__throw_ios_failure(char const*)'
/Users/jake/Library/Arduino15/packages/esp8266/hardware/esp8266/2.4.0/tools/sdk/lib/libstdc++.a(istream-inst.o): In function `__check_facet<std::ctype<char> >':
/Users/igrokhotkov/e/ESPTools/crosstool-NG/.build/xtensa-lx106-elf/build/build-cc-gcc-final/xtensa-lx106-elf/libstdc++-v3/include/istream:95: undefined reference to `std::__throw_bad_cast()'
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board Adafruit HUZZAH ESP8266.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Thanks in advance!
Thanks to Zebrafish for pointing me to this question about sstream with Arduino. Apparently it is difficult to include a lot of std c++ in Arduino projects, including stringstream.
This led me to find this answer, which recommends staying away from Arduino String. I used it to revise my function to this:
#include <string>
#include <cstring>
#include <map>
void SimpleKeyPairs::deserializeKeyPairs(std::string input, std::map<std::string, std::string>* output)
{
char* inputChars = const_cast<char*>(input.c_str());
char* key;
char* value;
key = strtok(inputChars, ":");
value = strtok(NULL, ";");
while(key != NULL && value != NULL) {
output->insert(std::make_pair(key, value));
key = strtok(NULL, ":");
value = strtok(NULL, ";");
}
}
I have a large code base that can use boost::any or boost::spirit::hold_any (depending on a macro definition).
hold_any seems to be compatible with boost::any (e.g. How to print boost::any to a stream? or Type erasure - Part IV) and faster (Why you shouldn’t use boost::any) but I'm experiencing several segmentation fault errors using hold_any (Boost v1.55 / 1.54 / 1.53).
This is a minimal working example that exhibits the same problem as the original code:
#include <iostream>
#include <string>
#include <vector>
#include <boost/spirit/home/support/detail/hold_any.hpp>
typedef boost::spirit::hold_any any;
typedef std::vector<any> vany;
int main()
{
vany data0, data1;
for (unsigned i(0); i < 1000; ++i)
{
std::string s("test_test_test");
data0.push_back(any(s));
}
const unsigned n(data0.size());
vany::iterator iter(data0.begin());
for (unsigned i(0); i < n; ++i)
{
std::cout << "Moving " << i << std::endl;
data1.push_back(*iter);
iter = data0.erase(iter);
}
return 0;
}
The program appears to work correctly:
changing from boost::spirit::hold_any to boost::any;
changing the content of the hold_any to a data type small enough to perform small buffer optimization (e.g. from std::string to int).
It seems strange that there could be some major bug in a widely used library such as Boost Spirit, but
I'm having a hard time finding a bug in the example;
I've tried g++ / clang++ without success.
What's wrong with the example?
You should not be using hold_any as it is in detail/hold_any.hpp for a reason.
That said, hold_any's copy-assignment appears to be broken. I've created a pull request on github with a proposed fix.
Without the fix, the following program demonstrates UB (because the compiler generates a shallow assignment operator which is preferred):
#include <iostream>
#include <string>
#include <boost/spirit/home/support/detail/hold_any.hpp>
typedef boost::spirit::hold_any any;
int main()
{
any b;
{
any a;
a = std::string("test_test_test");
b = a;
}
std::cout << "b: " << b << '\n';
}
When run under valgrind:
==11827== Invalid read of size 8
==11827== at 0x5E9D793: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std
==11827== by 0x4012FC: boost::spirit::detail::fxns<mpl_::bool_<true> >::type<std::string, char>::stream_out(std::ostream&, void* const*) (hold_any.hpp:113)
==11827== by 0x4010F5: std::basic_ostream<char, std::char_traits<char> >& boost::spirit::operator<< <char>(std::basic_ostream<char, std::char_traits<char> >&, boost::spirit::basic_hold_any<char> const&) (hold_any.hpp:368)
==11827== by 0x400FC9: main (test.cpp:17)
==11827== Address 0x8ac1650 is 0 bytes inside a block of size 39 free'd
==11827== at 0x4C2BADC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11827== by 0x5EC405E: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18)
==11827== by 0x401204: boost::spirit::detail::fxns<mpl_::bool_<true> >::type<std::string, char>::static_delete(void**) (hold_any.hpp:89)
==11827== by 0x401328: boost::spirit::basic_hold_any<char>::~basic_hold_any() (hold_any.hpp:246)
==11827== by 0x4010B4: boost::spirit::basic_hold_any<char>::~basic_hold_any() (hold_any.hpp:245)
==11827== by 0x400FA0: main (test.cpp:15)
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 8 years ago.
I have this bit of code from a stackoverflow question:
template <typename K, typename V>
bool exists_in(std::map<K,V> const& haystack, K const& needle)
{
return haystack.find(needle) != haystack.end();
}
Being a new user of templates, I still understand what is going on here. Only, I can't seem to apply it.
I have defined
class Varinfo; // meta information about vars
std::map<std::string,VarInfo*> g_varMap; // a map between var names and meta-info
In my main c++ code I have this statement:
// various other uses of g_varMap that don't cause errors then
if ( exists_in( g_VarMap, "fred" ) )
that generates this error.
undefined reference to `bool exists_in<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
VarInfo*>(std::map<std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, VarInfo*, std::less<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
std::allocator<std::pair<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const, VarInfo*> > > const&, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&)'
which I completely do not understand.
Can someone tell me why this is complaining? What bitof template knowledge am I missing? I have tried various casting operations on the variable returned by and sent to exists_in() including a std::string( "fred" ). Nothing helped. Some just generated even more meaningless errors.
Undefined reference means it cannot find the definition in any object file. This is a linker error. You need to define the template method within the header file and not in a separate cpp file.
Your code looks okay, I made a rough approximation, you can see it here: http://codepad.org/UvgeTIoC
If your function were available before the point of call you would have no problem. bear in mind that it is template code, so the full implementation has to be included either directly or indirectly:
#include <map>
#include <iostream>
template <typename K, typename V>
bool exists_in(std::map<K,V> const& haystack, K const& needle)
{
return haystack.find(needle) != haystack.end();
}
int main()
{
std::map<int, int> m{{1,1},{2,2}};
std::cout << std::boolalpha;
std::cout << exists_in(m,2) << "\n";
}
output true.
I can create .o file with g++ -c test.cpp -std=c++0x, but cant link it, got next errors:
test.cpp:(.text+0xe5): undefined reference to `std::regex_iterator<char const*, char, std::regex_traits<char> >::regex_iterator(char const*, char const*, std::basic_regex<char, std::regex_traits<char> > const&, std::bitset<11u>)'
test.cpp:(.text+0xf1): undefined reference to `std::regex_iterator<char const*, char, std::regex_traits<char> >::regex_iterator()'
Code:
#include <regex>
#include <iostream>
#include <string.h>
typedef std::regex_iterator<const char *> Myiter;
int main()
{
const char *pat = "axayaz";
Myiter::regex_type rx("a");
Myiter next(pat, pat + strlen(pat), rx);
Myiter end;
return (0);
}
The GNU C++ standard library supports <regex>, but not until version 4.9.0. (The headers were present in earlier versions, but were unusable.)
The other compilers don't support it, as far as I can see.
You can use a different library if you use an older GCC.