38

While I used to compile a single source file with Cmd+K in prior versions of Xcode, how does one do the same in Xcode 4? (Note that this is different than preprocessing or showing the disassembly of the file.) If compiling from a command line is proposed then it must be such that the project's settings, include paths, preprocessor definitions, etc., are all included.

An example use case is where I make a header file change but only want to test the change's effect with respect to a single source file, not all of the files that depend upon that header.

fbrereto
  • 35,429
  • 19
  • 126
  • 178
sergey
  • 579
  • 1
  • 5
  • 9
  • Since it appears Apple has completely removed this feature from Xcode 4, I think the only way we'll get it back is to make an Xcode plug-in. Unfortunately, the plug-in API isn't documented and Apple hasn't made any statements about opening it up anytime soon. – inspector-g May 06 '12 at 04:15
  • 2
    i have the same problem. i like using this sort of thing as a method for porting - i bring over the files from another platform and build them to see where the errors are i need to fix on a per-file basis. – jheriko Jun 15 '12 at 09:25
  • You should accept John Stephen's answer. – ksl Oct 05 '15 at 07:01

6 Answers6

13

The command has been moved to the Perform Action submenu. Look under

Product > Perform Action > Compile filename.cpp

To assign Cmd+K to it, go to

File > Preferences > Key Bindings > Product Menu group

and you'll find Compile File where you can assign a key. Cmd+K is assigned to Clear Console now by default, so be sure to remove that binding to avoid conflicts.

John Stephen
  • 7,625
  • 2
  • 31
  • 45
7

One way that I have found to do this is to using the following menu commands:

Product -> Generate Output -> Generate Preprocessed File
Product -> Generate Output -> Generate Assembly File

This may not be exactly what you want, but it will compile the single file.

Dave Ferguson
  • 1,456
  • 13
  • 17
  • 2
    In Xcode 5 you have `Product -> Perform Action -> Compile "filename.cpp"` (make sure the source file is selected and added to the active target). – Emerald Weapon Jul 18 '14 at 09:57
-1

When you build a project, xcode runs compilation command. You can check the log, search for your file and copy paste that command on Terminal. It'll compile only the file for which you copy/pasted on the terminal.

mAc
  • 199
  • 1
  • 3
  • 9
-2

The keyboard shortcut Cmd+K on Xcode 3 and before has been remapped to Cmd+B on Xcode 4

Along the same lines, Cmd+Return was remapped to Cmd+R (in case you ever used that)

oey192
  • 13
  • 4
  • 1
    Compiling a single file is not the same as building the whole project. – fbrereto May 02 '12 at 16:27
  • @fbrereto I think oey192 realizes that, but people with 1 karma can't post comments. – Mahmoud Al-Qudsi May 07 '12 at 16:52
  • @MahmoudAl-Qudsi I see; I didn't get that impression as Cmd+K (compile a single source file in Xcode 3) isn't the same operation as building the whole project (Cmd+B on both Xcode 3 and 4.) Maybe I'm just reading his answer wrong... – fbrereto May 07 '12 at 20:51
  • The way I understand it, he's pointing out that the previous keyboard shortcut, even if it appears to work, doesn't because it's been remapped to solution build. – Mahmoud Al-Qudsi May 07 '12 at 20:53
-2

If your file is C (or C++) file, then simply open your terminal, go to the folder in which the file resides and type

gcc -o outputFile inputFile.c

I am not familar with Objective-c that much, but GCC might work since it's only a superset of C, just like C++.

Hope that was helpful :)

Rorchackh
  • 2,113
  • 5
  • 22
  • 38
  • Not really what he asked for and yes, gcc can compile it as long objective C support is compiled in and necessary library includes are provided. – Tomas Pruzina Mar 29 '12 at 09:09
-3

The common requirement for single file compilation is checking it for syntax errors. (atleast for me). Since xcode4 highlights syntax errors as you type. It seems apple removed that feature.

user602592
  • 117
  • 1
  • 10
  • Compiling a single file is about a lot more than syntax errors. Among other things it will show you whether that file has its dependencies declared adequately. – Kaitain Feb 25 '16 at 00:17