M
M
Muriam2019-04-18 07:24:19
linux
Muriam, 2019-04-18 07:24:19

How to fix error when compiling .cpp file via linux ROSA terminal?

This code compiles fine in DEV-C++ if the one-line comments are removed.

/* добавить два элемента к массиву и посчитать кол-во элементов */
#include <iostream>
//#include <conio.h>
#include <vector>
#include <locale.h> 

using namespace std;

int main()
{	
  setlocale(LC_ALL, "rus");
  
    int rez;
    int arr_src[] = {1,8,3,2};
    std::vector<int> arr(arr_src, arr_src + sizeof(arr_src)/sizeof(arr_src[0]));

    arr.push_back(7);		//добавление элемента "7"
    arr.push_back(8);		//и "8"

    rez = arr.size();          				
    cout << "кол-во элементов в массиве: " << rez << endl;
    
    cout << "массив: ";
    for (int i = 0; i < rez; i++)
        cout << arr[i] << ' ';
        
        
//    getch();
    return 0;
}

[email protected] ~ $ gcc vector.cpp -o vector
/tmp/ccCANy65.o: In function `main':
vector.cpp:(.text+0xc4): undefined reference to `std::cout'
vector.cpp:(.text+0xc9): 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*)'
vector.cpp:(.text+0xd9): undefined reference to `std::ostream::operator<<(int)'
vector.cpp:(.text+0xde): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
vector.cpp:(.text+0xe6): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
vector.cpp:(.text+0xf0): undefined reference to `std::cout'
vector.cpp:(.text+0xf5): 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*)'
vector.cpp:(.text+0x122): undefined reference to `std::cout'
vector.cpp:(.text+0x127): undefined reference to `std::ostream::operator<<(int)'
vector.cpp:(.text+0x134): 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/ccCANy65.o: In function `__static_initialization_and_destruction_0(int, int)':
vector.cpp:(.text+0x1b1): undefined reference to `std::ios_base::Init::Init()'
vector.cpp:(.text+0x1c0): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccCANy65.o: In function `std::vector<int, std::allocator<int> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&)':
vector.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[_ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi]+0x25f): undefined reference to `__cxa_begin_catch'
vector.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[_ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi]+0x2c8): undefined reference to `__cxa_rethrow'
vector.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[_ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi]+0x2d0): undefined reference to `__cxa_end_catch'
/tmp/ccCANy65.o: In function `std::vector<int, std::allocator<int> >::_M_check_len(unsigned long, char const*) const':
vector.cpp:(.text._ZNKSt6vectorIiSaIiEE12_M_check_lenEmPKc[_ZNKSt6vectorIiSaIiEE12_M_check_lenEmPKc]+0x4c): undefined reference to `std::__throw_length_error(char const*)'
/tmp/ccCANy65.o: In function `__gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long)':
vector.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim[_ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim]+0x1c): undefined reference to `operator delete(void*)'
/tmp/ccCANy65.o: In function `__gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*)':
vector.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv]+0x2c): undefined reference to `std::__throw_bad_alloc()'
vector.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv]+0x3c): undefined reference to `operator new(unsigned long)'
/tmp/ccCANy65.o:(.eh_frame+0x4b): undefined reference to `__gxx_personality_v0'
collect2: ошибка: выполнение ld завершилось с кодом возврата 1

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Radjah, 2019-04-18
@Muriam

I quote the second result in Google according to the text of the error:

Because cout is present in the C++ standard library, which requires gcc to explicitly link with -lstdc++; g++ links the standard library by default.
gcc vector.cpp -lstdc++ -o vector.o
The first one is about the same, but in English.

F
Fedor, 2019-04-18
@keine-lust

Compile c++ code with c compiler. Bravo)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question