【问题标题】:Problem occurred while compiling a cpp program in gcc在 gcc 中编译 cpp 程序时出现问题
【发布时间】:2011-04-08 05:52:24
【问题描述】:

我的代码是

#include<iostream>   

using namespace std;  
int main()  
{  
   cout <<"hi";  
   return 0;  
}

我编译它:

gcc d.cpp -o d  

错误如下

Undefined                       first referenced
 symbol                             in file
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator [](unsigned int) const/var/tmp//ccyDVhhA.o
std::cout                           /var/tmp//ccyDVhhA.o
std::ios_base::Init::~Init()        /var/tmp//ccyDVhhA.o
std::ios_base::Init::Init()         /var/tmp//ccyDVhhA.o
__gxx_personality_v0                /var/tmp//ccyDVhhA.o
std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_tr aits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)/va r/tmp//ccyDVhhA.o
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() c onst/var/tmp//ccyDVhhA.o
ld: fatal: Symbol referencing errors. No output written to d
collect2: ld returned 1 exit status

上面的代码有什么错误?

【问题讨论】:

标签: c++


【解决方案1】:

您应该使用g++ 而不是gcc 编译您的C++ 程序。代码没有错误,但是如果您使用带有这些参数的 gcc,那么您的程序将不会链接到 C++ 标准库。

【讨论】:

    【解决方案2】:
    g++ d.cpp -o d 
    

    应使用 g++ 进行 c++ 程序编译以链接到 C++ 标准库。

    【讨论】:

    • 等等,真的吗?我以为 GCC 会自动检测类型? (我尝试编译一个 Java 文件,它说 Java 不受支持...)
    • @Mehrdad - 我不确定我们是否可以配置 gcc 来做到这一点。但默认情况下,它不能链接到 C++ 库。
    • @Mahesh:是的,很奇怪……很有趣。
    • @Mehrdad:确实如此。阅读stackoverflow.com/questions/172587/…
    • @Martin:链接不是说gcc will compile: *.c/*.cpp files as C and C++ respectively.吗?
    【解决方案3】:

    该代码应该可以使用 c++ 编译器正常编译,但是您使用 gcc(c 编译器)编译它需要使用 g++

    【讨论】:

      猜你喜欢
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      相关资源
      最近更新 更多