macos 如何从我的 Mac 上的终端窗口运行 g++

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

How to run g++ from a terminal window on my Mac

c++macosgccg++

提问by ragnarius

I have a simple C++ program that I can compile (g++ from command line) and run from my work-computer but not from my home-computer. I don't know how they differ! They are both macbooks and I have installed iPhone SDK on both.

我有一个简单的 C++ 程序,我可以编译(从命令行使用 g++)并在我的工作计算机上运行,​​但不能从我的家用计算机上运行。不知道有什么区别!它们都是 macbook,我都安装了 iPhone SDK。

On my home-computer: When I add /Developer/usr/binto my PATHI can run g++ but it cannot include <string>and it does not recognize printfand such functions. What else do I need to do to make g++ work?

在我的家用计算机上:当我添加/Developer/usr/bin到我的计算机时,我PATH可以运行 g++,但它不能包含<string>并且无法识别printf此类功能。我还需要做什么才能使 g++ 工作?

回答by Mircea Prodan

  1. Install xCode and then close
  2. Edit your c++ code file (I use Smultron, it is free and good)
  3. Save your file on Desktop with "cpp" termination
  4. Suppose you have test.cpp program
  5. Open Terminal (Applications -> Utilities)
  6. Change directory with command: cd Desktop
  7. Now write: g++ test.cpp -o test
  8. For run your program, in Terminal type: test (only that, without ending cpp!)
  9. If your program it is well writen, on the desktop appear a new file named test
  10. Click on it and you have your program running
  11. That is all!
  1. 安装 xCode 然后关闭
  2. 编辑你的 C++ 代码文件(我使用 Smultron,它是免费的而且很好)
  3. 使用“cpp”终止将文件保存在桌面上
  4. 假设你有 test.cpp 程序
  5. 打开终端(应用程序 -> 实用程序)
  6. 使用命令更改目录:cd Desktop
  7. 现在写: g++ test.cpp -o test
  8. 要运行您的程序,请在终端中键入:test(仅此而已,不结束 cpp!
  9. 如果你的程序写得很好,桌面上会出现一个名为 test 的新文件
  10. 单击它,您的程序就会运行
  11. 就这些!

回答by Stuart Golodetz

When you installed Xcode and the iPhone SDK, did you check the box for Unix Development Support? I had a lot of problems with this sort of thing because I didn'tdo that, and the solution was to uninstall the SDK and start again with the box checked.

当您安装 Xcode 和 iPhone SDK 时,您是否选中了 Unix Development Support 复选框?我在这类事情上遇到了很多问题,因为我没有这样做,解决方案是卸载 SDK 并重新开始,选中该框。