chipKIT® Development Platform

Inspired by Arduino™

STL in STD lib linker issue? ( #include <list> )

Created Thu, 05 Sep 2013 06:41:54 +0000 by unexpectedly


Thu, 05 Sep 2013 06:41:54 +0000

I'm trying to use <list> from the STL. Warning: this is my first real adventure into the murky depths of C++ and STD library or the STL. I have avoided OOP my entire life and I guess it's time to enter the 2000's.

Everything is ok until actually try to add something to the list with push_back(), which was the most recent line of code added:

struct MenuItem {
		char		label[DISPLAY_WIDTH];
		void*		target;
	MenuItem drive = { "Step right", reinterpret_cast&lt;void*&gt;(&amp;stepRight) };
	std::list&lt;MenuItem&gt;  _menus;

	addMenuItem( MenuItem newItem ){

	void setup() {

	void stepRight( void ) {
		//  working code that moves the stepper motor...
		rotate( 100, CCW );

... and then with the push_back() added, here's the compiler error output:

mfm\mfm.cpp.o: In function `std::list&lt;MenuItem, std::allocator&lt;MenuItem&gt; &gt;::_M_insert(std::_List_iterator&lt;MenuItem&gt;, MenuItem const&amp;)':
C:/Users/chris/Documents/mpide-0023-windows-20130715/hardware/pic32/compiler/pic32-tools/pic32mx/include/c++/4.5.1/bits/stl_list.h:1435: undefined reference to `std::_List_node_base::_M_hook(std::_List_node_base*)'
collect2: ld returned 1 exit status

In myfirstmenu mfm class, I thought I'd use <list> to store a list of the menu item structs. I pretty much add one line of code, compile, fix my errors, compile again. This error is the first to go so deep into the libraries.

I googled for it and this was the most relevant result.

Some of the above pseudo-code is in classes. The exact code I'm trying to compile is in the attached .zip file and will compile if you open up Libraries/mfm/mfm.cpp and go to line 15 and comment out the line with push_back() in it.

This Stack Overflow QA made me question my usage, but tutorials on the internet tell me to start / build to / insert into a <list> to use push_back()

Is this a potential linker problem or (hopefully) is there something I'm doing wrong? Any thoughts about steps I'd take next to try and fix it? They're talking about .so binary objects in the linker related to version of the linker...? If the only solution within the next few days is to "just use prev and next* in the struct and do it C style"*, I'm ok with that, too. Done those before, back in my bitter anti-oop days. :D

Thanks, Chris


Thu, 05 Sep 2013 09:07:19 +0000

How about using a vector instead if a list? You have to define an allocation error callback routine with it - there's an old thread here somewhere with the details.