Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Help in running Code::Blocks on Mac OSX. Should I use something else?

I'm starting to learn programming and can't seem to get Code::Blocks running on my lap top. Terminal says "Can't execute binary file." I'm trying to build the first "Hello World" C++ program but it would not build or run. Sometimes I get the message that it cannot access folder also.
Here's my code:

#include <iostream>
using namespace std;
int main()
{
cout << "Hello World !" << endl;
}


What C++ software do you recommend to start? Should I use any other than Code::Blocks?

A1181, Mac OS X (10.5.2), Black MacBook

Posted on Dec 3, 2009 2:42 PM

Reply
Question marked as Best reply

Posted on Dec 3, 2009 2:59 PM

Use [XCode|http://developer.apple.com/technology/tools.html].
18 replies

Dec 4, 2009 3:02 AM in response to xnav

As xnav said, Xcode supports C, C++, Objective-C, and code that contains a combination of C++ and Objective-C (often referred to as Objective-C++). I believe it also supports languages like Fortran and Java and some others, but I don't have any experience with that.

Xcode is the application that comes with Mac OS X that Mac users are meant to write their code with and develop with. There are certainly other options out there, but I think you'll find Xcode to be sufficient for most anything you need to do, and there are a lot of plus-sides to using it, since it's what Apple wants you to use (for example, the documentation is unmatched, as is the support, as you are hopefully finding out). Unless you find something that you can't do with Xcode, in which case you will obviously have to go elsewhere, then I definitely recommend sticking with Xcode.

To program a standard C++ command-line program using Xcode:

1.) Open Xcode
2.) Go to File->New Project...
3.) Under "User Templates" on the left, select "Command Line Utility"
4.) Select "C++ Tool"

Hope that helps!

Dec 5, 2009 2:49 AM in response to ericmeyers

I can't tell you how many times I've said compile and link to somebody and they say "what button do I click?" 😟


Guilty as charged, Eric. But eager to learn. By "an editor and the command line", you mean a text editor (like TextEdit or something) and the Terminal, right? I know that code in a language like C is compiled into machine language/native code/object code (difference?), which is then linked together to form the application, but I don't know how to compile and link code from the Terminal. So, without taking too much of your time (without hijacking this thread), can you (or anyone) enlighten me to a short extent on how I would take C code from TextEdit (a .txt file, I assume?) and compile/link it using the Terminal? It's actually something I've been curious about for awhile, so if anyone gets a quick moment to break it down for me, I'd be appreciative. Thanks!

Dec 5, 2009 4:49 AM in response to Tron55555

I agree with Eric completely here. The only way to really know what is happening is to do it via the command line.

Here is a basic outline of steps, I'm sure people will correct me on this.

1) Create a directory/folder for your program
2) Create your source files (myprogram.c). You can use any editor you want for this, but do save it with a .c extension (.cpp for C++). Place the source files into the directory you made
3) From command line, cd (change directory) into the folder you made.
4) Here is where people will start disagreeing with me. At this point you can type 'gcc -o myprogram myprogram.c' and it will probably work (assuming you don't need to link anything, more on that later). I prefer the two step approach, compile and then link. Issuing the command 'gcc -c myprogram.c' will compile the program, and output an object file (with a .o extension). The command 'gcc -o myprogram myprogram.o' will take the object file and link it, turning it into myprogram.
5) Run your program via terminal (./myprogram).

Now, you may find out that you have to link to libraries such as the math library if you are using any functions found in math.h. To do this, append -lm to the end of the linking command for gcc. The '-l' part says to link against a library and the 'm' tells it to use the math library.

Also, since you really don't want to type in these commands every time you want to compile, consider writing a Makefile. There are plenty of tutorials online for writing Makefiles. I also recommend the O'Reilly book "Managing Projects with GNU Make." That book walks you through the gcc commands I mentioned above also.

Dec 5, 2009 6:10 PM in response to Tron55555

As far as text editors, TextEdit will work, but as far as I know, you will constantly run into issues with formatting, spell-check, and other things. If you are guilty of "Which Button Do I Press?" then my first choice is likely not for you.

If you are comfortable using the "Terminal" app, then I suggest using EMACS, (or just emacs). There is a built-in tutorial, and it is a very powerful editor, although for a non command line user it can be daunting, as most of the formatting and editing commands are all control characters (so regular characters are always entered as text). Emacs will do nice highlighting of various language elements just like XCode does. vi is another command line editing tool available on every version of UNIX. Emacs comes with Mac OS/X, as does vi. I have no idea if vi helps with programming as the first thing I do when I start to work on any Unix or Linux box is copy over some version of emacs, so i don't use it except sparingly.

You can also continue to use XCode to edit the source files, as it does a nice job as a WYSIWYG editor and prompts you with help along the way. Just keep a terminal window open to use the command line to compile and link your program, but edit it in XCode and do a SAVE before you switch back to the terminal window.

I also use BBEdit from Bare Bones, and some people like TextWrangler. There are at least a dozen other text editors out there that have features you can enable for programming. Do a google search for Mac OSX text editors and you'll see what I mean.

I advise AGAINST using TextEdit, unless you set the file to "Plain Text". Also, don't use a .txt extension. Use .c, .cc, .ccp, .m, .mm, .js, or whatever the compiler expects to see for a specific language. Binaries will nearly always have a .o extension, and the final binary depends on the language, the linker and what options you provide. The default for c is a.out (or at least it used to be).

You will also want to learn how to use "make", but that is another discussion.

For starters, remember that "man" is your friend. If you want to look up information on Unix, and the command line on the Mac is talking to Unix, aka Darwin. Type "man man" for help getting started with the man command, and "man make" to get started with make. Note at the end of most man pages the section that says "See also", as this tells you about related commands.

Dec 6, 2009 4:21 AM in response to reststop

Well, I didn't want to take over this thread, but the original poster doesn't seem to be revisiting the thread, so...

Thanks a lot, reststop -- that was a great post. I think I'm going to keep using Xcode as the text editor for now. I had BBEdit on a previous installation of OS X (Tiger), but I only used it for HTML. Anyways, I learned a lot from that, so thank you.

etresoft -- I typed in "xed" at the command line, which appeared to allow me to use the Xcode editor from the command-line, as you said. Two quick things about that, though, if you don't mind: first, do I simply type in my code in the same manner I would in the GUI version of the editor? Secondly, assuming that the answer is yes, how do I "enter" the code once I'm done. After I wrote a few lines, I had to ctrl-C out because I couldn't figure out how to enter the code I'd written. I'm sure it's something obvious, but my experience with the Terminal isn't massive.

Thanks again, guys. If I have any more questions about this, I'll start a new thread that is properly titled for the sake of future searchers.

Dec 6, 2009 4:44 PM in response to Tron55555

Tron55555 wrote:
etresoft -- I typed in "xed" at the command line, which appeared to allow me to use the Xcode editor from the command-line, as you said. Two quick things about that, though, if you don't mind: first, do I simply type in my code in the same manner I would in the GUI version of the editor? Secondly, assuming that the answer is yes, how do I "enter" the code once I'm done. After I wrote a few lines, I had to ctrl-C out because I couldn't figure out how to enter the code I'd written. I'm sure it's something obvious, but my experience with the Terminal isn't massive.


I don't understand your question. That is why I always recommend people start with the Terminal instead of Xcode. It is better to know the fundamentals of what is going on behind the scenes. Once you create a file (using xed, vi, TextWranger, or whatever) you can run GCC (or some other compiler) to compile it. The act of compiling it will create a .o object file or an executable file (depending on your options).

Dec 7, 2009 3:20 AM in response to etresoft

I don't understand your question.


That's probably because it's not understandable. 🙂

In all seriousness (well, actually I was being serious), but let me try again. First off, as far as learning Terminal first and understanding the fundamentals of what's going on behind the scenes, I totally agree with you. Unfortunately, however, I didn't know to agree with you when I first started learning, so that's why I'm trying to learn now. So, I think I'll take a detour from my C/Objective-C/Cocoa travels and get that Cocoa book that Bob (I think it was Bob) recommended so that I can get some understanding there.

So, I'm not going to try to rephrase my question too much, because I just don't have the understanding necessary to speak with any intelligence on this. However, what I will ask is this: how do you "enter" something in the Terminal once it enters that multi-line mode? In other words, I'm used to hitting enter when I want to register what I've typed with the Terminal after typing a command, but after typing "xed" it simply goes to a newline every time I hit enter, so how do you achieve this? I ended up hitting ctrl+C to get back to the command prompt. Anyways, if it's still not clear, just ignore me -- I won't be able to do anything with the information anyways until I understand more. I was just curious.

Help in running Code::Blocks on Mac OSX. Should I use something else?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.