【问题标题】:C++ newbie, getting compile error (linux, g++ compiler)C++ 新手,出现编译错误(linux、g++ 编译器)
【发布时间】:2017-06-19 05:08:02
【问题描述】:

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

【问题讨论】:

标签: c++ compiler-errors compilation


【解决方案1】:

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

【讨论】:

  • 答案是正确的+1,但给他多一点解释会更好
猜你喜欢
  • 2012-07-03
  • 2013-03-13
  • 2015-02-16
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多