C++ 对“std::cout”的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28236870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:56:06  来源:igfitidea点击:

undefined reference to 'std::cout'

c++c++11gcccout

提问by D1X

Shall this be the example:

难道这就是例子:

#include <iostream>
using namespace std;
int main()
{
    cout << "Hola, moondo.\n";
}

It throws the error:

它抛出错误:

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

Also, this example:

另外,这个例子:

#include <iostream>
int main()
{
    std::cout<<"Hola, moondo.\n";
}

throws the error:

抛出错误:

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

Note: I am using Debian Wheezy.

注意:我使用的是 Debian Wheezy。

回答by shauryachats

Compile the program with:

使用以下命令编译程序:

g++ -Wall -Wextra -Werror -c main.cpp -o main.o
     ^^^^^^^^^^^^^^^^^^^^ <- For listing all warnings when your code is compiled.

as coutis present in the C++ standard library, which would need explicit linkingwith -lstdc++when using gcc; g++links the standard library by default.

正如coutC++ 标准库中存在的那样,在使用时需要显式链接; 默认链接标准库。-lstdc++gccg++

With gcc, (g++should be preferred over gcc)

gcc, (g++应该优先于gcc)

gcc main.cpp -lstdc++ -o main.o

回答by A B

Yes, using g++command worked for me:

是的,使用g++命令对我有用:

g++ my_source_code.cpp

回答by iggy12345

Makefiles

生成文件

If you're working with a makefile and you ended up here like me, then this is probably what you're looking or:

如果您正在使用 makefile 并且像我一样最终到达这里,那么这可能就是您要查找的内容或:

If you're using a makefile, then you need to change ccas shown below

如果您使用的是makefile,则需要进行cc如下更改

my_executable : main.o
    cc -o my_executable main.o

to

CC = g++

my_executable : main.o
    $(CC) -o my_executable main.o