GCC 4.4.3编译器链接问题

时间:2013-03-13 06:38:55

标签: c++ gcc linker

好的,所以我想回到c ++,并开始使用基本程序。

#include <iostream>

using namespace std;

int main()
{
 cout << "So This Is It.";
 cout << "\n";
 return 0;
}

这就是结果:

-->gcc -g -o HELLO HELLO.cpp
/tmp/ccLZLm5N.o: In function `main':
/root/Programming/Learning/C++/HELLO.cpp:9: undefined reference to `std::cout'
/root/Programming/Learning/C++/HELLO.cpp:9: 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*)'
/root/Programming/Learning/C++/HELLO.cpp:10: undefined reference to `std::cout'
/root/Programming/Learning/C++/HELLO.cpp:10: 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*)'
/tmp/ccLZLm5N.o: In function `__static_initialization_and_destruction_0':
/usr/include/c++/4.4/iostream:72: undefined reference to `std::ios_base::Init::Init()'
/usr/include/c++/4.4/iostream:72: undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccLZLm5N.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

我的编译器:

-->gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

我可以请一些关于修复的信息吗?要么。如果我遇到任何更简单的语法问题,如何在此找到正确的信息?

1 个答案:

答案 0 :(得分:3)

您使用了C编译器而不是C++编译器。 gcc代表C,g++代表C ++。 GCC的链接器将链接到C标准库,该库不包含C ++标准库提供的函数,这会导致未定义的引用。

因此请使用正确的g++

g++ -g -o HELLO HELLO.cpp

补充说明:

通常gcc标识C ++源代码。但是,如果您未自行安装,则最有可能是您的Ubuntu安装没有安装g++。要求您的主管安装g++或自己安装g++

sudu apt-get install g++

由于您已经在安装内容,因此请gdbvalgrind进行调试。