GCC and C++11...no friends?

Kevin Ingwersen ingwie2000@googlemail.com
Sun Feb 2 16:05:00 GMT 2014


Hey!

I just wanted to try out lambdas and picked up an example from the internet:

#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
 
int main() {

    std::vector<int> c { 1,2,3,4,5,6,7 };
    int x = 5;
    c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; } ), c.end());
 
    std::cout << "c: ";
    for (auto i: c) {
        std::cout << i << ' ';
    }
    std::cout << '\n';
 
    std::function<int (int)> func = [](int i) { return i+4; };
    std::cout << "func: " << func(6) << '\n'; 
}

Then, I went to compile this:

	gcc test.cpp -o test -std=c++11

The result:

/tmp/cc4Rcd5b.o: In function `main':
test.cpp:(.text+0xdf): undefined reference to `std::cout'
test.cpp:(.text+0xe4): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
test.cpp:(.text+0x132): undefined reference to `std::cout'
test.cpp:(.text+0x137): undefined reference to `std::ostream::operator<<(int)'
test.cpp:(.text+0x144): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)'
test.cpp:(.text+0x17a): undefined reference to `std::cout'
test.cpp:(.text+0x17f): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)'
test.cpp:(.text+0x1b7): undefined reference to `std::cout'
test.cpp:(.text+0x1bc): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
test.cpp:(.text+0x1c6): undefined reference to `std::ostream::operator<<(int)'
test.cpp:(.text+0x1d3): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char)'
/tmp/cc4Rcd5b.o: In function `std::_Function_base::_Base_manager<main::{lambda(int)#2}>::_M_clone(std::_Any_data&, std::_Function_base::_Base_manager<main::{lambda(int)#2}> const&, std::integral_constant<bool, false>)':
test.cpp:(.text+0x7a3): undefined reference to `operator new(unsigned long)'
/tmp/cc4Rcd5b.o: In function `std::_Function_base::_Base_manager<main::{lambda(int)#2}>::_M_destroy(std::_Any_data&, std::integral_constant<bool, false>)':
test.cpp:(.text+0x7df): undefined reference to `operator delete(void*)'
/tmp/cc4Rcd5b.o: In function `std::_Function_base::_Base_manager<main::{lambda(int)#2}>::_M_init_functor(std::_Any_data&, {lambda(int)#2}&&, std::integral_constant<bool, false>)':
test.cpp:(.text+0x808): undefined reference to `operator new(unsigned long)'
/tmp/cc4Rcd5b.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x863): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0x872): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc4Rcd5b.o:(.rodata+0x30): undefined reference to `vtable for __cxxabiv1::__class_type_info'
/tmp/cc4Rcd5b.o: In function `std::function<int (int)>::operator()(int) const':
test.cpp:(.text._ZNKSt8functionIFiiEEclEi[_ZNKSt8functionIFiiEEclEi]+0x21): undefined reference to `std::__throw_bad_function_call()'
/tmp/cc4Rcd5b.o: In function `__gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long)':
test.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim[_ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim]+0x1c): undefined reference to `operator delete(void*)'
/tmp/cc4Rcd5b.o: In function `__gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*)':
test.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv]+0x2c): undefined reference to `std::__throw_bad_alloc()'
test.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv]+0x3c): undefined reference to `operator new(unsigned long)'
/tmp/cc4Rcd5b.o:(.eh_frame+0x12b): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status

Why isn’t this working? :o

Kind regards, Ingwie


More information about the Gcc-help mailing list