код компилируется в VS на windows, но не компилируется в Linux

Рейтинг: 0Ответов: 1Опубликовано: 05.06.2023

Я создал тестовый test.cpp файл для проверки корректности компиляции через gcc терминала Linux системы.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> a = {0,1,2,3,4,5};
    for (int i = 0; i < a.size(); ++i) {
     cout << a[i] << " ";
     }
    return 0;
}

В VisualStudio вывод именно такой, какой я ожидаю. Но после выполнения команды:

gcc test.cpp -o a.out

Я получаю страшный для меня вывод:

/usr/bin/ld: /tmp/ccikM5i3.o: warning: relocation against _ZSt4cout' in read-only section .text'
/usr/bin/ld: /tmp/ccikM5i3.o: in function `main':
test.cpp:(.text+0xb9): undefined reference to `std::cout'
/usr/bin/ld: test.cpp:(.text+0xc1): undefined reference to `std::ostream::operator<<(int)'
/usr/bin/ld: test.cpp:(.text+0xd6): 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*)'
/usr/bin/ld: /tmp/ccikM5i3.o: in function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x196): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: test.cpp:(.text+0x1b1): undefined reference to `std::ios_base::Init::~Init()'
/usr/bin/ld: /tmp/ccikM5i3.o: in function `std::vector<int, std::allocator<int> >::_S_check_init_len(unsigned long, std::allocator<int> const&)':
test.cpp:(.text._ZNSt6vectorIiSaIiEE17_S_check_init_lenEmRKS0_[_ZNSt6vectorIiSaIiEE17_S_check_init_lenEmRKS0_]+0x65): undefined reference to `std::__throw_length_error(char const*)'
/usr/bin/ld: /tmp/ccikM5i3.o: in function `__gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long)':
test.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim[_ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim]+0x2f): undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: /tmp/ccikM5i3.o: in function `__gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*)':
test.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv]+0x49): undefined reference to `std::__throw_bad_array_new_length()'
/usr/bin/ld: test.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv]+0x4e): undefined reference to `std::__throw_bad_alloc()'
/usr/bin/ld: test.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv]+0x5e): undefined reference to `operator new(unsigned long)'
/usr/bin/ld: /tmp/ccikM5i3.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

И я понимаю, что скорее всего с кодом все в порядке и это просто какие-то варнинги, но я не понимаю, как это исправить.

Ответы

▲ 2

g++ автоматически линкует исполняемый файл со стандартной библиотекой c++.

Для gcc нужно явно указывать эту библиотеку: gcc test.cpp -o a.out -lstdc++

И кстати, не нужно указывать -o a.out. Это имя присваивается исполняемому файлу автоматически. Достаточно gcc test.cpp -lstdc++