这是示例吗?
#include <iostream>
using namespace std;
int main()
{
cout << "Hola, moondo.\n";
}
它引发错误:
gcc -c main.cpp gcc -o edit main.o main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): 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*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)':
main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
另外,此示例:
#include <iostream>
int main()
{
std::cout<<"Hola, moondo.\n";
}
引发错误:
gcc -c main.cpp gcc -o edit main.o main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): 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*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)': main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
注意:我正在使用Debian Wheezy。
好吧,那肯定解决了问题。据我了解,GCC是Gnu Compiler Collection的首字母缩写。需要时不应该调用g ++编译器吗?因此,命令gcc改为调用c编译器...
—
D1X 2015年
@ D1X是因为您从编译器中单独调用了链接器。在编写时,
—
MM
gcc -o edit main.o
它不知道main.o
将需要C ++启动库。
问:是否在需要时调用g ++编译器?答:根据需要,调用gfortran,gjc,...等的方法不超过gcc。
—
paulsm4
g++
代替gcc
。gcc
是针对C的,不会让您访问C ++标准库。