C++ 通过命令行在 OS X 上编译简单的 Hello World 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4073289/
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
Compiling simple Hello World program on OS X via command line
提问by erikvold
I've got a simple hello world example that I'm trying to compile on OS X, named hw.cpp
:
我有一个简单的 hello world 示例,我正在尝试在 OS X 上编译它,名为hw.cpp
:
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Hello world!" << endl;
return 0;
}
I'd like to compile it using gcc
, but I've had no success. I'd also like to hear the other options, like using Xcode ?
我想使用 编译它gcc
,但我没有成功。我还想听听其他选项,比如使用 Xcode 吗?
回答by Martin York
Try
尝试
g++ hw.cpp
./a.out
g++
is the C++ compiler frontend to GCC.gcc
is the C compiler frontend to GCC.
g++
是 GCC 的 C++ 编译器前端。gcc
是 GCC 的 C 编译器前端。
Yes, Xcode is definitely an option. It is a GUI IDE that is built on-top of GCC.
是的,Xcode 绝对是一个选择。它是一个建立在 GCC 之上的 GUI IDE。
Though I prefer a slightly more verbose approach:
虽然我更喜欢稍微详细一点的方法:
#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
}
回答by nobody
g++ hw.cpp -o hw
./hw
回答by Sam Miller
user@host> g++ hw.cpp
user@host> ./a.out
回答by Ignacio Vazquez-Abrams
Compiling it with gcc
requires you to pass a number of command line options. Compile it with g++
instead.
编译它gcc
需要你传递一些命令行选项。编译它g++
。
回答by Badmanchild
The new version of this should read like so:
新版本应该是这样的:
xcrun g++ hw.cpp
./a.out
回答by José Rojas
Also, you can use an IDE like CLion (JetBrains) or a text editor like Atom, with the gpp-compiler plugin, works like a charm (F5 to compile & execute).
此外,您可以使用像 CLion (JetBrains) 这样的 IDE 或像 Atom 这样的文本编辑器,带有 gpp-compiler 插件,就像一个魅力(F5 编译和执行)。
回答by wLc
Use the following for multiple .cpp files
对多个 .cpp 文件使用以下内容
g++ *.cpp
./a.out
回答by Stephen Canon
You didn't specify what the error you're seeing is.
您没有指定您看到的错误是什么。
Is the problem that gcc
is giving you an error, or that you can't run gcc
at all?
是问题导致gcc
您出错,还是根本无法运行gcc
?
If it's the latter, the most likely explanation is that you didn't check "UNIX Development Support" when you installed the development tools, so the command-line executables aren't installed in your path. Re-install the development tools, and make sure to click "customize" and check that box.
如果是后者,最可能的解释是您在安装开发工具时没有勾选“UNIX 开发支持”,因此您的路径中没有安装命令行可执行文件。重新安装开发工具,并确保单击“自定义”并选中该框。