I've got a simple hello world example that I'm trying to compile on OS X, named 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 ?
Try
g++ hw.cpp
./a.out
g++
is the C++ compiler frontend to GCC.
gcc
is the C compiler frontend to GCC.
Yes, Xcode is definitely an option. It is a GUI IDE that is built on-top of GCC.
Though I prefer a slightly more verbose approach:
#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
}
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?
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.
Use the following for multiple .cpp files
g++ *.cpp
./a.out
Compiling it with gcc
requires you to pass a number of command line options. Compile it with g++
instead.
user@host> g++ hw.cpp
user@host> ./a.out
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).
The new version of this should read like so:
xcrun g++ hw.cpp
./a.out
g++ hw.cpp -o hw
./hw
Source: Stackoverflow.com