Compiling multiple Objective-C files on the command line with clang

Hope a simple question. I am trying to learn the basic Objective-C compilation from the command line with clang. I understand that Xcode is the best solution for complex projects, and I plan to switch to it soon, but personally it seems to me that I understand the language better if I can compile it manually in the terminal, and for smaller introductory software projects I find it less hassle to compile in the terminal than launching a new project.

So the question is: how do I compile an Objective-C program that consists of several files (from the command line)? (I'm trying to run the fractions program from Chapter 7 of Kochan’s tutorial with the main.m and Fraction.m files, which are both #import "Fraction.h" and the Fraction.h file that imports the Foundation framework). To compile a single file, I use something like

clang -fobjc-arc main.m -o prog1 

But if I type this and I want to include files other than main.m in the project, I get errors, including:

 ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 

If I try to add additional files to the command as arguments, for example

 clang -fobjc-arc main.m Fraction.h Fraction.m -o prog1 

then i get

 clang: error: cannot specify -o when generating multiple output files 

And if I remove the -o argument, for example

 clang -fobjc-arc main.m Fraction.h Fraction.m 

then I get a gigantic bunch of errors related to the underlying structure, ending up with a “fatal error: too many errors were emitted, stopping now.”

How do I do this and make it work? Or do I just need to suck it and use Xcode as soon as more than one file is involved?

+7
source share
2 answers

Compiled only implementation files (* .m), not interface / include files, so this should work:

 clang -fobjc-arc main.m Fraction.m -o prog1 

The strange error message “creating multiple output files” is probably due to the fact that clang can also compile header files into “precompiled header files”.

+6
source

Do not specify Fraction.h as the source file. This is not a header file.

You also almost certainly want to quit the -framework Foundation . If you use any other frameworks, you probably also need to specify them.

+2
source

All Articles