C++新手,得到编译错误(Linux,G ++编译器)

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

本文关键字:Linux 编译器 错误 新手 编译 C++      更新时间:2023-10-16

我只是在学习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

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