C++ Compiling on openwrt

i am trying to compile a simple .cpp helloworld program to a x86 virtual machine using the toolchain.
the staging_dir is located at: "source\staging_dir\toolchain-i386_pentium4_gcc-5.4.0_musl-1.1.16" (source is the root installation folder of openwrt)
inside it there is a bin folder which has lots of compilers such as: i486-openwrt-linux-gcc, i486-openwrt-linux-c++, etc...

first, how should i know which one to use?
second, i already tried using i486-openwrt-linux-gcc to compile a simple helloworld looks like this:

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
	//printf("Hell! O' world, why won't my code compile?\n\n");
	string s="hello_cpp!\n";
	cout << s;
	return 0;
}

but compilation ends with errors. this is what it shows:

rotem@rotem_haimov:~/source/staging_dir/toolchain-i386_pentium4_gcc-5.4.0_musl-1.1.16$ ./bin/i486-openwrt-linux-gcc -o hello helloworld.cpp
/tmp/ccI3JX2E.o: In function `main':
helloworld.cpp:(.text+0x1a): undefined reference to `std::allocator<char>::allocator()'
helloworld.cpp:(.text+0x32): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
helloworld.cpp:(.text+0x41): undefined reference to `std::allocator<char>::~allocator()'
helloworld.cpp:(.text+0x50): undefined reference to `std::cout'
helloworld.cpp:(.text+0x55): undefined reference to `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::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
helloworld.cpp:(.text+0x69): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
helloworld.cpp:(.text+0x7e): undefined reference to `std::allocator<char>::~allocator()'
helloworld.cpp:(.text+0x9a): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/tmp/ccI3JX2E.o: In function `__static_initialization_and_destruction_0(int, int)':
helloworld.cpp:(.text+0xd4): undefined reference to `std::ios_base::Init::Init()'
helloworld.cpp:(.text+0xe9): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccI3JX2E.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status

If you haven't already read it, I'd start with https://openwrt.org/docs/guide-developer/helloworld/start which goes over how to get a simple C program compiled within the build environment.

It's also not entirely clear what your goals are when you say, "to a x86 virtual machine" -- the OpenWRT build environment is not general-purpose cross-compiler.

1 Like

The answer you want is here: https://fleshandmachines.wordpress.com/2011/08/22/openwrt-cc-programing/