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

BigDogUK

macrumors newbie
Original poster
Apr 4, 2005
5
0
Hey all.

I'm starting a little bit of coding on OSX and only started learning recently. I was wondering if there is any way to compile a single C++ file in XCode without having to create a project.

Just on Windows I was using Context and a .bat file so I could just press F5 and it'd compile with the correct file name. Saves me having to go to console etc.

So is this possible using XCode? And/or is there another way to do this easily?
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
XCode isn't well suited for tiny projects like that. Actually I'm not aware of any simple IDE on the Mac that are suitable for this kind of thing ... I'll do some searching ...
 

Robin Forder

macrumors newbie
Jun 11, 2009
1
0
Cambridge, UK
Compile a single C++ file

You could try right/ctrl clicking on the document. In most cases, the context sensitive menu will show a compile option (below Get Info). Right clicking on the file name in the Groups & Files menu should work if that doesn't.

http://www.monen.nl/DevDoc/document...bs_building_product/chapter_34_section_3.html & see Compiling Individual Files.

Also, there's a thing called Predictive Compilation that can be switched on in the building pane of the preferences, which compiles as you code:

http://developer.apple.com/document....html#//apple_ref/doc/uid/TP40002695-CBHJCIIF.
 

sammich

macrumors 601
Sep 26, 2006
4,305
268
Sarcasmville.
This wins:

Code:
make [I]filename[/I]

That's the file name without the extension. For example: to compile main.cc, just type "make main".
Your program will be called main. It's a shortcut for g++ -o main main.cc
 

Some Guy 555

macrumors regular
May 26, 2009
194
0
I conquer with the make command, its useful however in large applications (and especially in upper classes in post secondary) its advisable to make the compiler display more than just a few errors.

This is my favorite.

For C++

g++ filename.cpp -o name -Wall

The above compiles a C++ file by the name of filename.cpp and names the executable name. What is different here is the -Wall command, which is short for with all warnings/errors.

Very useful to display many possible errors that would not be made clear with a default compile.
 

dioscw

macrumors newbie
Mar 8, 2010
1
0
This wins:

Code:
make [I]filename[/I]

That's the file name without the extension. For example: to compile main.cc, just type "make main".
Your program will be called main. It's a shortcut for g++ -o main main.cc

Whit this method I can compile my source file perfect, but with the gcc sourcefile.c -target -Wall only create and a.out file..... what I am doing wrong?

I need to compile simple C files for a basic university programing course and my laptop computer is a mac.

Thanks!
 

notjustjay

macrumors 603
Sep 19, 2003
6,056
167
Canada, eh?
Whit this method I can compile my source file perfect, but with the gcc sourcefile.c -target -Wall only create and a.out file..... what I am doing wrong?

Nothing. "a.out" is the compiled executable generated from your C code. Rename it to something else (main, or whatever) as you wish.

Run it in Terminal by typing

Code:
./a.out
 

ChrisA

macrumors G5
Jan 5, 2006
12,581
1,697
Redondo Beach, California
Whit this method I can compile my source file perfect, but with the gcc sourcefile.c -target -Wall only create and a.out file..... what I am doing wrong?

Nothing wrong. if you don't sepify a name the default name is "a.out" To run it just type ./a.out

If you want to specify a name use the "-o" switch

gcc myfile.c -o myfile

Also use you other switches like -wall or -g if you like But if you leave off the -o gcc uses the default
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.