New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

How to compile (without building) a .c/.cpp file with Xcode?


Post Reply New Thread Subscribe

 
Thread Tools
ZhimingWang

 
ZhimingWang's Avatar
 
Member Since: Aug 03, 2012
Posts: 22
ZhimingWang is on a distinguished road

ZhimingWang is offline
First, I'll post my two questions at the very beginning in case you don't want to listen to me nagging about my personal circumstances:

1. How to compile without building a .c/.cpp file? Is it possible?
2. How to run my file without rebuilding? (Seems that Xcode builds the file before running whether or not the file has been built? Don't tell me to export the executable and run, though; I mean is there a command in Xcode...)

Environment: Xcode 4.4.1 on Mountain Lion.

OK, let me explain in details:
I'm switching from Dev C++ to Xcode. Yes, this is a silly question, but you know, beginners usually stuck with silly questions...X_X

Well, I'm running Xcode 4.4.1 on Mountain Lion. I have the habit to compile codes every several minutes to shorten the annoying debugging process in the end. However, I can't find the compile option. I can choose to build the file (I am working in a project, not a single file), but you know, building takes way more time than compiling, and it is not economical to build each time if the source code is considerably long. So here comes my first question:

1. How to compile without building a .c/.cpp file? Is it possible?

By the way, when I finished a .c/.cpp file, I Cmd+B to build it and succeeds. But when I Cmd+R to run, before the program started the "Building Succeeds" message appeared on the screen. Does it mean the file underwent the building process again? If it is so, it will waste a considerable amount of time, right? So here comes my second question:

2. How to run my file without rebuilding?

Thank you in advance of your kind help!
QUOTE Thanks
Raz0rEdge

 
Raz0rEdge's Avatar
 
Member Since: Jul 17, 2009
Location: MA
Posts: 7,576
Raz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant future
Mac Specs: 27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3

Raz0rEdge is online now
What exactly does "compile without building" even mean? A quick primer on how this all works. A program can be comprised one one or multiple sources/header files..

If there is a single source file, it is first pre-processed by the compiler to evaluate all of the "#include", "#import", "#define", "#pragma" and other pre-processor directives. The file is then compiled into an object file, it is then finally linked against any necessary libraries to make it a functioning program.

The same process holds true for a project with multiple source files, you go through all the steps for each of the source files and deal with any cross-file dependencies as needed or report errors if things are messed up. Once it's all done, the final executable is created for execution.

Suppose you have a project with 3 source files, you modify one of them and then hit CMD+b, this will cause the compiler to determine which file changed, and go through the steps I described above and create the executable at that point. If you happen to do a CMD+r immediately after, the build will automatically run but since it's already up to date, nothing really happens except the message appearing and going to execute your program.

I'll have to check to see if there is a XCode option to prevent building when running, but that is usually a problem if you don't remember to build before you run.

Unless you are writing a VERY complicated program or running this on a Mac that is severely lacking in power, I can't imagine the few seconds it takes for the incremental builds or the minute it might take build the entire program is a huge deal.

I deal with building things that takes hours to complete so I know all about waiting..

--
Regards
...Ashwin



Be sure to read the Community Guidelines | The more information you provide, the better answers you get, remember GIGO.
QUOTE Thanks
ZhimingWang

 
ZhimingWang's Avatar
 
Member Since: Aug 03, 2012
Posts: 22
ZhimingWang is on a distinguished road

ZhimingWang is offline
Quote:
Originally Posted by Raz0rEdge View Post
What exactly does "compile without building" even mean? A quick primer on how this all works. A program can be comprised one one or multiple sources/header files..

If there is a single source file, it is first pre-processed by the compiler to evaluate all of the "#include", "#import", "#define", "#pragma" and other pre-processor directives. The file is then compiled into an object file, it is then finally linked against any necessary libraries to make it a functioning program.

The same process holds true for a project with multiple source files, you go through all the steps for each of the source files and deal with any cross-file dependencies as needed or report errors if things are messed up. Once it's all done, the final executable is created for execution.

Suppose you have a project with 3 source files, you modify one of them and then hit CMD+b, this will cause the compiler to determine which file changed, and go through the steps I described above and create the executable at that point. If you happen to do a CMD+r immediately after, the build will automatically run but since it's already up to date, nothing really happens except the message appearing and going to execute your program.

I'll have to check to see if there is a XCode option to prevent building when running, but that is usually a problem if you don't remember to build before you run.

Unless you are writing a VERY complicated program or running this on a Mac that is severely lacking in power, I can't imagine the few seconds it takes for the incremental builds or the minute it might take build the entire program is a huge deal.

I deal with building things that takes hours to complete so I know all about waiting..
Hi Ashwin,

Thank you for your really long reply

As for "compile without building", I mean the process that you compile the source file, generate the .obj file but don't link them, so no executables are generated. I mainly use this for grammatical check. (Seems that Xcode does this as I'm typing? Will that kind of check work in complex situations - I mean, will Xcode point out all errors in syntax?) I previously worked on Dev C++ on PC, and there was the option to compile, OR build. If my memory doesn't fail me, it usually takes five to six times longer to build a project than to compile it...

Anyway, I'm merely a college undergraduate and an amateur coder, so it typically take no longer than half a minute to build my projects. The waiting time becomes annoying only in the debugging process. Well, neither compiling without building nor running without rebuilding helps in debugging, though, unless I miss the bug and have to step into the same executable again...
QUOTE Thanks
Raz0rEdge

 
Raz0rEdge's Avatar
 
Member Since: Jul 17, 2009
Location: MA
Posts: 7,576
Raz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant future
Mac Specs: 27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3

Raz0rEdge is online now
Xcode does indeed parse your code as you are typing and will point out blatant syntax errors or missing imports, declarations or unused variables and so on. It you fix most of these things before you compile, then the errors that are encountered are compile/linking errors that couldnt have been detected by a syntax check anyway..

--
Regards
...Ashwin



Be sure to read the Community Guidelines | The more information you provide, the better answers you get, remember GIGO.
QUOTE Thanks
ZhimingWang

 
ZhimingWang's Avatar
 
Member Since: Aug 03, 2012
Posts: 22
ZhimingWang is on a distinguished road

ZhimingWang is offline
Quote:
Originally Posted by Raz0rEdge View Post
Xcode does indeed parse your code as you are typing and will point out blatant syntax errors or missing imports, declarations or unused variables and so on. It you fix most of these things before you compile, then the errors that are encountered are compile/linking errors that couldnt have been detected by a syntax check anyway..
Got it. Thank you Ashwin!
QUOTE Thanks
smallmountain

 
Member Since: Aug 12, 2013
Posts: 2
smallmountain is on a distinguished road

smallmountain is offline
Could this be correct? XCode will not let you simply compile a .cpp file to its object file without building the entire project? Visual Studio lets you do this. It is very helpful when you are changing a header file, say with templates in it, that is widely included in your project, but only one or two source files use the template class or function that you are trying to get right. I like to compile the .cpp file to make sure I don't have a stupid error, and then once the .cpp file compiles, then I fire up a build that might take 10 minutes. Maybe I have link errors after the build, so I repeat the cycle, but at least I am not waiting 10 minutes to find out I have a simple error.
QUOTE Thanks
smallmountain

 
Member Since: Aug 12, 2013
Posts: 2
smallmountain is on a distinguished road

smallmountain is offline
My understanding is that with XCode 5, you will be able to compile a single C++ file just like you can in Visual Studio.
QUOTE Thanks
cptkrf

 
Member Since: Dec 08, 2009
Location: The same as Sheldon Cooper - East Texas
Posts: 394
cptkrf is a jewel in the roughcptkrf is a jewel in the rough
Mac Specs: MacBook Air 2013, MacMini,2013, Intel Core i7, 16gb, 27" Thunderbolt display (Highly recommended!)

cptkrf is offline
You can drop to a terminal and just compile the c/cpp source with GCC just as if you were on a plain Unix or Linux machine.

In fact, if you are making c/cpp programs you don't need Xcode at all, just download the Command Line Utilities and use textedit for the source. (Note. if you are making gui obj-c stuff, you have to have Xcode.)

The simplest compile will be with the gcc command and the source file name. i.e. gcc helloworld.c

man gcc will give you the manual for the options.
QUOTE Thanks

Post Reply New Thread Subscribe


« NSlog C99 error. | Objective-C methods »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Issue building program using Xcode Timo116 OS X - Development and Darwin 0 07-20-2012 03:11 PM
Is Apple ready to play cat and mouse with malware developers? OneMoreThing... Apple Rumors and Reports 3 06-04-2011 10:10 PM
Problem with building up C++ project in Xcode feng.gu OS X - Development and Darwin 2 03-10-2008 02:13 PM
Can you guys open this site? Desolate One OS X - Apps and Games 13 04-29-2005 01:23 PM
Xcode building apps PinkPhishDoors OS X - Development and Darwin 1 09-30-2004 10:50 AM

All times are GMT -4. The time now is 10:55 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?