开发者

What is the process gone through during Objective-C compilation

I'm currently having some linker problems when trying to compile an Objective-C program and think that the reason why I can't figure out the issue may be due to ignorance as to the compiler process.

Would it be possible for somebody to give me an overview of the steps taken during compilation?

This is as I currently开发者_StackOverflow understand the process:

  1. The compiler copies the contents of any included .h files into the file that it was defined in. The compiler does not keep track of whether a .h file has already been included, so it may be included within a project multiple times.

  2. Any .m files are compiled to C equivalent code (which are in turn compiled to object code).

  3. The linker produces links between the declarations made in the .h files and the appropriate functions within the object code. The appropriate functions are determined by looking for them in a .m file of the same name.

  4. The object files are connected together to form the executable, making sure that the main function is situated at the entry point of the executable. Any declarations are then presumably deleted to save space?

Assuming this is correct (which it may not be), this would presumably mean that you should never #include .m files because you will likely end up with multiple method definitions which will cause the linker problems.

Thanks for any illumination anybody can bring to this :).

Cheers,

Danny


You get the idea more or less correctly. A few corrections:

  1. #include doesn't check whether it's already included or not, but #import does check.

  2. .m is not first converted to C and then to object code. It was done that way 20 years ago, but it's no longer the case. It's just directly compiled down to object code.

  3. The linker doesn't care how the file was named. You can use different file names for .h and .m. You can split implementations of functions declared in a .h file into multiple .m files, for example.

  4. Whether unused implementations are deleted or not depends on the compiler and the compiler options.

In any case, your conclusion is correct: you should never include/import an implementation file to another implementation file. You will run into a double-implementation error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜