Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RBrownT

macrumors newbie
Original poster
Dec 27, 2011
2
0
Hi,
I've just barely started (yesterday) experimenting with C++ and it has brought me to Xcode. I've been following a tutorial that seems to be very well written, but sadly, it is mainly directed towards Windows for things such as "Finding an IDE. They did recommend Xcode, and I have gotten to the first program "Hello World!". The problem is that I cannot seem to find out to build and run the code that I have copied and pasted. I've looked around, and there doesn't seem to be very much help up for versions 4.x, so can anyone help me?
 

robvas

macrumors 68040
Mar 29, 2009
3,240
629
USA
Hi,
I've just barely started (yesterday) experimenting with C++ and it has brought me to Xcode. I've been following a tutorial that seems to be very well written, but sadly, it is mainly directed towards Windows for things such as "Finding an IDE. They did recommend Xcode, and I have gotten to the first program "Hello World!". The problem is that I cannot seem to find out to build and run the code that I have copied and pasted. I've looked around, and there doesn't seem to be very much help up for versions 4.x, so can anyone help me?

Shift-Command-Y will bring up the output window of the debugging console. Your program is probably writing there.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Hi,
I've just barely started (yesterday) experimenting with C++ and it has brought me to Xcode. I've been following a tutorial that seems to be very well written, but sadly, it is mainly directed towards Windows for things such as "Finding an IDE. They did recommend Xcode, and I have gotten to the first program "Hello World!". The problem is that I cannot seem to find out to build and run the code that I have copied and pasted. I've looked around, and there doesn't seem to be very much help up for versions 4.x, so can anyone help me?

To run your own code: Create a new project using the "Command Line Tool, C++" template: File->New->New Project, then Pick MacOS X, Application, Command Line Tool, Next, change "Type" to C++. This creates a new project with "Hello world" in main.cpp. Command-R to run it.

In the main window, check what the three buttons above "View" do. The middle own shows/hides the debug/output area.
 

robvas

macrumors 68040
Mar 29, 2009
3,240
629
USA
Also, since your Mac is a UNIX machine, you can just work from the terminal. You can also use other text editors such as TextWrangler, Komodo, or SublimeEdit to edit the code, and then compile and run them from the terminal. You don't want to use TextEdit or Word - they could save your program code as a file other than text, which will cause problems.

Code:
Rocket:Documents robert$ cat hello.c
#include <stdio.h>
 
int main(void)
{
  printf("Hello world\n");
  return 0;
}

Rocket:Documents robert$ gcc hello.c -o hello
Rocket:Documents robert$ ./hello
Hello world

If you haven't gotten XCode, you'll have to download it (it's free) from Apple. Check out http://developer.apple.com for more information.
 
Last edited by a moderator:

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
You might also try going IDE-less and use gcc/clang directly. (EDIT: robvas beat me to it).

Learning how to edit, compile, link and execute programs from the command line first is a good skill to learn.

B
 

RBrownT

macrumors newbie
Original poster
Dec 27, 2011
2
0
Thanks, for the suggestions, if I was going to continue without an IDE, is there and application that comes preloaded on the Mac, and if so, how would I run it from that app? (Sorry, I know it's a noob question.)
 
Last edited:

mmendoza27

macrumors 6502
Oct 18, 2007
350
48
Thanks, for the suggestions, if I was going to continue without an IDE, is there and application that comes preloaded on the Mac, and if so, how would I run it from that app? (Sorry, I know it's a noob question.)

Yeah, it's called Terminal. It's basically the command line interface (CLI) that comes with the Mac. It can be found in your Applications > Utilities folder. It's basically like Windows Command Prompt. Have you worked with command line before?

I'd highly recommend learning how to navigate and run your program in terminal. As a second year CS student, it's just good to know and you don't need all the cruft of a IDE for simple programs. I've had some people in my classes use Eclipse and it just adds complication to what should be your focus, programming syntax and using efficient algorithms.
 

thundersteele

macrumors 68030
Oct 19, 2011
2,984
9
Switzerland
I do use Xcode for my small C++ projects, however I just use it as text editor. Then I compile and run my code in the console.

Xcode is very powerful for GUI development with Cocoa and objective C, but for learning the basics of programming I think it just adds an extra layer of problems.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.