Engine
Engine

Reputation: 141

C++ newbie, getting compile error (linux, g++ compiler)

I'm just learning C++ and I'm trying to compile a very simple program.

I've tried looking around the web and I have no idea what's wrong or why the error is saying that I have undefined references to various variables.

I'm using the g++ compiler on Linux.

Here is the code:

#include <iostream>
using namespace std;

int main()

{

    double celsius_temp, fahrenheit_temp;

    cout << "Input celsius temp:";
    cin >> celsius_temp; 
    fahrenheit_temp = (celsius_temp * 1.8) + 32; 
    cout << "Fahrenheit temp is:" << fahrenheit_temp;
    return 0;

}

Here was the command entered and the error:

$ gcc convert.cpp -o convert

/tmp/ccTjTVCH.o: In function `main':
convert.cpp:(.text+0x1d): undefined reference to `std::cout'
convert.cpp:(.text+0x22): 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*)'
convert.cpp:(.text+0x2e): undefined reference to `std::cin'
convert.cpp:(.text+0x33): undefined reference to `std::istream::operator>>(double&)'
convert.cpp:(.text+0x5f): undefined reference to `std::cout'
convert.cpp:(.text+0x64): 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*)'
convert.cpp:(.text+0x7c): undefined reference to `std::ostream::operator<<(double)'
/tmp/ccTjTVCH.o: In function `__static_initialization_and_destruction_0(int, int)':
convert.cpp:(.text+0xbe): undefined reference to `std::ios_base::Init::Init()'
convert.cpp:(.text+0xcd): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

Upvotes: 1

Views: 1049

Answers (1)

Utkarsh
Utkarsh

Reputation: 181

Compile usingg++ instead of gcc. The errors are linker errors(It cannot find the function references). gcc can also compile cpp but it is complicated

Upvotes: 4

Related Questions