macos C++:在 mac osx 上使用 std::cout 和 gcc
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2250017/
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
C++: Using std::cout with gcc on a mac osx
提问by Patof
I'm new to C++ programming and I try to make my first exercise on a mac using gcc in the terminal.
我是 C++ 编程的新手,我尝试在终端中使用 gcc 在 mac 上进行我的第一个练习。
Unfortunately, I can't compile because of issues related to iostream. With a simple program as:
不幸的是,由于与 iostream 相关的问题,我无法编译。用一个简单的程序作为:
#include <iostream>
int main()
{
std::cout << "hello world";
std::cout << endl;
return 0;
}
it gives me the error:
它给了我错误:
error: ‘endl' was not declared in this scope
removing the cout << endl; line gives me these errors:
删除 cout << endl; 行给了我这些错误:
Undefined symbols:
"___gxx_personality_v0", referenced from:
___gxx_personality_v0$non_lazy_ptr in cceBlyS2.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in cceBlyS2.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in cceBlyS2.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in cceBlyS2.o
"std::cout", referenced from:
__ZSt4cout$non_lazy_ptr in cceBlyS2.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
It's evident that the iostream header is not properly linked. I tried "<"iostream.h">" and "iostream.h" with no success.
很明显,iostream 标头没有正确链接。我尝试了 "<"iostream.h">" 和 "iostream.h",但没有成功。
Does anybody has any hint that could help me? Thanks!
有人有任何提示可以帮助我吗?谢谢!
回答by Jerry Coffin
You need to use std::endl;
-- the entire standard library is in the std
namespace. It also looks like you used gcc
instead of g++
on the command line. The latter automatically does the steps necessary to link C++ correctly.
您需要使用std::endl;
——整个标准库都在std
命名空间中。它看起来也像是您在命令行上使用的gcc
而不是g++
。后者会自动执行正确链接 C++ 所需的步骤。
回答by Buhake Sindi
endl;
falls under the std
namespace
endl;
属于std
命名空间
your 2 options are as follows:
您的 2 个选项如下:
1) declaring your namespace, e.g.
1)声明你的命名空间,例如
#include <iostream>
using namespace std;
int main() {
cout << "hello world";
cout << endl;
return 0;
}
or using std::endl;
e.g.
或使用std::endl;
例如
std::cout << "hello world";
std::cout << std::endl;
return 0;
See which one suits you. I recommend 1) (Check that I didn't do std::cout
because I've declared my namespace already) as it helps to reduce typing std::
everytime.
看看哪一款适合你。我推荐 1)(检查我没有这样做,std::cout
因为我已经声明了我的命名空间)因为它有助于减少std::
每次输入。
回答by e.James
You just need to use std::endl;
. Or, better yet, make use of the handy using
directive:
您只需要使用std::endl;
. 或者,更好的是,使用方便的using
指令:
#include <iostream>
using namespace std;
int main()
{
cout << "hello world";
cout << endl;
return 0;
}