0

I'm new to C++ and i'm copying this exactly from my textbook to CS50.

#include <iostream>

using namespace std;

int main () {

    cout << "My first C++ program." << endl;
    cout << "The sum of 2 and 3 = " << 5 << endl;
    cout << "7+8 =" << 7+8 <<endl;

    return 0;
}

When i they to compile using : gcc sample.cpp -g -o sample

I get the following error:

/tmp/ccaTrTqq.o: In function `main':
/home/ubuntu/workspace/Chapter 1/sample.cpp:7: undefined reference to `std::cout'
/home/ubuntu/workspace/Chapter 1/sample.cpp:7: 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*)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:7: 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> >&)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:7: undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/home/ubuntu/workspace/Chapter 1/sample.cpp:8: undefined reference to `std::cout'
/home/ubuntu/workspace/Chapter 1/sample.cpp:8: 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*)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:8: undefined reference to `std::ostream::operator<<(int)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:8: 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> >&)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:8: undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/home/ubuntu/workspace/Chapter 1/sample.cpp:9: undefined reference to `std::cout'
/home/ubuntu/workspace/Chapter 1/sample.cpp:9: 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*)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:9: undefined reference to `std::ostream::operator<<(int)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:9: 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> >&)'
/home/ubuntu/workspace/Chapter 1/sample.cpp:9: undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/ccaTrTqq.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.8/iostream:74: undefined reference to `std::ios_base::Init::Init()'
/usr/include/c++/4.8/iostream:74: undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
user4581301
  • 33,082
  • 7
  • 33
  • 54
Alcantara
  • 35
  • 4
  • 7
    Use `g++` instead of `gcc` to compile C++ files and build an executable. `g++ sample.cpp -g -o sample` – R Sahu Jun 07 '18 at 17:20
  • 3
    `gcc` --> `g++`. Also Read https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Achal Jun 07 '18 at 17:20
  • 1
    `using namespace std;` is bad practice. You're better off using `std::cout` every time you need to call it. – emsimpson92 Jun 07 '18 at 17:23
  • https://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc explains the difference well – rerun Jun 07 '18 at 17:27
  • Using g++ solved the issue thank you guys! – Alcantara Jun 07 '18 at 17:29

0 Answers0