I
I
Issue2021-11-02 21:10:22
linux
Issue, 2021-11-02 21:10:22

What's wrong with compiling on linux?

I want to learn how to write in C++, but from the very first compilation there are a lot of incomprehensible problems. To the point that I deleted everything and copied Hello World from w3schools . But here are the same problems.

Here is the source:

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  return 0;
}


Here is the builder script:
#!/usr/bin/bash
gcc main.cpp -o main
chmod 777 -R ./main
./main


And finally a ton of errors in the terminal:
/usr/bin/ld: /tmp/cc4X6rkd.o: in function `main':
main.cpp:(.text+0x12): undefined reference to `std::cout'
/usr/bin/ld: main.cpp:(.text+0x17): 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/cc4X6rkd.o: in function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x4b): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: main.cpp:(.text+0x60): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
./build.sh: line 4: ./main: No such file or directory


Tell me what's wrong? Maybe this is a sign, and you need to throw the computer out the window?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Syomov, 2021-11-02
@paulenot

You must use g++ or gcc -lstdc++.
But a computer in the window is also a rich idea. =)
PS Also, chmod does not need the -R switch, and 777 is unnecessary, u+x is enough.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question