在Mac上编译C ++代码 [英] Compiling C++ code on Mac

查看:168
本文介绍了在Mac上编译C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Mac上写非常简单的C ++程序,但我得到错误。我没有很多C ++(和gcc)的经验,网上的简单指南也不讨论这个话题。

I want to write very simple C++ programs on my Mac but I get errors. I don't have a lot of experience with C++ (and gcc) and the simple guides on the web also don't discuss this topic.

请看看我的简单hello世界程序:

Please have a look at my simple hello world program:

erik2:~[03:38 pm]$ cat hw.cpp 
#include <iostream>

int main ()
{
  cout<<"Hello World!";
  return 0;
}
erik2:~[03:38 pm]$ gcc hw.cpp 
hw.cpp: In function ‘int main()’:
hw.cpp:5: error: ‘cout’ was not declared in this scope

),我放回(!)命名空间声明,但这不会导致成功,虽然:

Responding to the answers until now (thanks a lot), I put back(!) the namespace declaratation, but that doesn't result in a success, though:

erik2:~[03:51 pm]$ cat hw.cpp 
using namespace std;
#include <iostream>

int main ()
{
  cout<<"Hello World!";
  return 0;
}
erik2:~[03:51 pm]$ gcc hw.cpp 
Undefined symbols:
  "___gxx_personality_v0", referenced from:
      ___gxx_personality_v0$non_lazy_ptr in ccphDFtO.o
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in ccphDFtO.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 ccphDFtO.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int)in ccphDFtO.o
  "std::cout", referenced from:
      __ZSt4cout$non_lazy_ptr in ccphDFtO.o
ld: symbol(s) not found
collect2: ld returned 1 exit status


推荐答案

您需要添加

using namespace std;

或限定您在

std::cout << "Hello, world!";

因此使符号可见(您还要添加换行....)。

so make the symbol visible (and you also want to add a newline....).

这篇关于在Mac上编译C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆