未定义引用'std :: cout' [英] undefined reference to 'std::cout'
本文介绍了未定义引用'std :: cout'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是例子:
#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。
Note: I am using Debian Wheezy.
推荐答案
使用以下代码编译程序:
Compile the program with:
g++ -Wall -Wextra -Werror -c main.cpp -o main.o
^^^^^^^^^^^^^^^^^^^^ <- For listing all warnings when your code is compiled.
as cout
当使用 gcc
时,需要<$>显式链接与 -lstdc ++
g ++
默认链接标准库。
as cout
is present in the C++ standard library, which would need explicit linking with -lstdc++
when using gcc
; g++
links the standard library by default.
使用 gcc
, g ++
应优先于 gcc
)
gcc -lstdc++ main.cpp -o main.o
这篇关于未定义引用'std :: cout'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文