非常简单的程序不能在 C++ 中工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9447428/
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-27 12:50:55  来源:igfitidea点击:

Very simple program not working in c++?

c++cout

提问by spatara

I can't figure out why this isn't working...

我不明白为什么这不起作用......

I am working in Linux

我在 Linux 工作

g++ doesn't do anything

g++ 什么都不做

gcc prints the following:

gcc 打印以下内容:

/tmp/ccyg7NDd.o: In function `main':
test.cc:(.text+0x14): undefined reference to `std::cout'
test.cc:(.text+0x19): 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*)'
test.cc:(.text+0x21): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cc:(.text+0x29): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccyg7NDd.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cc:(.text+0x51): undefined reference to `std::ios_base::Init::Init()'
test.cc:(.text+0x56): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccyg7NDd.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

Code:

代码:

#include<iostream>
#include<stdio.h>

int main(){

std::cout<<"test "<<std::endl;
return 0;

};

回答by kev

gcc main.cpp -lstdc++

or

或者

g++ main.cpp

回答by hmjd

gccis the C compiler, you need to use g++(or use gccwith option -lstdc++as pointed out by others). If by nothing is printed after you use g++is what you mean, you have to execute the compiled binary after you build it (i.e. when g++completes).

gcc是 C 编译器,您需要使用g++(或gcc-lstdc++其他人指出的选项一起使用)。如果你使用后什么都不打印g++就是你的意思,你必须在构建后(即g++完成时)执行编译后的二进制文件。

main.cpp:

主.cpp:

#include<iostream>

int main(){

std::cout<<"test "<<std::endl;
return 0;

};

Build:

建造:

g++ main.cpp -o main

Execute:

执行:

./main

Output:

输出:

test 

回答by sank

This is a C++ code and so you should use g++ and not gcc. Also #include<stdio.h> is not needed

这是一个 C++ 代码,所以你应该使用 g++ 而不是 gcc。也#include<stdio.h> 没有必要

回答by Thomas Padron-McCarthy

I think you are mistakenly linking with the C compiler command instead of the C++ compiler command. Try this:

我认为您错误地与 C 编译器命令而不是 C++ 编译器命令链接。尝试这个:

g++ test.cc -o test

g++ test.cc -o 测试

回答by jjdaybr

I think the underlying issue for this is that the binary name "test" is actually already apart of the linux system. Typing in "man test" displays a manual for the test binary. I had the EXACT same issue. It was resolved simply by compiling the binary to something other than "test".

我认为这个问题的根本问题是二进制名称“test”实际上已经是 linux 系统的一部分。输入“man test”会显示测试二进制文件的手册。我有完全相同的问题。只需将二进制文件编译为“测试”以外的内容即可解决。

回答by gFu

Compilers delimit by whitespace. Put a space between all keywords and <<. So cout<< should be cout <<. Same with <

编译器用空格分隔。在所有关键字和 << 之间放置一个空格。所以 cout<< 应该是 cout <<。与 < 相同