C ++新手,得到编译错误(linux,g ++编译器)

时间:2017-06-19 05:08:02

标签: c++ compiler-errors compilation

我只是在学习C ++,而我正在尝试编写一个非常简单的程序。

我试过环顾网络,我不知道出了什么问题,或者为什么错误说我对各种变量都有未定义的引用。

我在Linux上使用g ++编译器。

以下是代码:

#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;

}

这是输入的命令和错误:

$ 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

1 个答案:

答案 0 :(得分:4)

使用g++代替gcc进行编译。错误是链接器错误(它找不到函数引用)。 gcc也可以编译cpp,但它很复杂