OC http://www.jianshu.com/p/c2dabb4fd761
The syntax of block looks special, but it is actually handled as very ordinary C language code. Here we borrow the ability of the clang compiler: the ability to convert it into our readable source code. The console command is: clang -rewrite-objc source code filename.
1
2
3
4
5
|
int
main(){
void
(^blk)(
void
) = ^{printf(
"block\n"
);};
blk();
return
0
;
}
|
After clang -rewrite-objc, the code programming looks like this (after simplifying the code, readers can search for keywords to find in the generated file):
We all know that we can learn the Objective-C programming language through Xcode , but can we learn Objective-C without the IDE of XCode? Of course it is possible.
As a programmer, you should know that any programming language is inseparable from a compiler, and OC is no exception. We can find through Du Niang’s search that the default compiler of XCode is clang, so the question is, can I directly compile and run a piece of OC code through the clang command? Of course it is possible.
For a detailed introduction to the XCode compiler, please refer to this article: Compiler
The following will describe how to create a Hello Word program through the text editor in Mac OS, and compile and run it through the clang command.
step 1
Open the "Text Editing" tool, enter the following code, and save it in plain text format. The file name is HelloWord.m, and the path is optional.
<code class="objectivec"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68);">#import <span class="hljs-title"><Foundation/Foundation.h></span></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> main(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> argc, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">char</span> * argv[]) { <span class="hljs-keyword" style="color: rgb(0, 0, 136);">@autoreleasepool</span> { <span class="hljs-built_in" style="color: rgb(102, 0, 102);">NSLog</span>(<span class="hljs-string" style="color: rgb(0, 136, 0);">@"Hello, OC!"</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>; }</code>
步骤2
接下来可以利用“终端”将HelloWord.m文件编译成可执行文件了,具体步骤:
1.打开“终端”
2.通过cd命令进入HelloWord.m文件所在目录
3.使用clang命令对HelloWord.m文件进行编译
最后生成HelloWord可执行文件,如下图所示。
$ clang -fobjc-arc -framework Foundation HelloWord.m -o HelloWord
有几个地方需要注意一下:
- $符号是终端命令提示符,不是需要输入的内容
- -fobjc-arc表示编译器需要支持ARC特性
- -framework Foundation表示引用Foundation框架
- HelloWord.m为需要进行编译的源代码文件
- -o HelloWord indicates the file name of the output executable file
step 3
After the executable file is generated, the file can be executed in the terminal, and the command entered is as follows:
$ ./HelloWord
The execution result is as follows:
In addition, you can directly double-click to run the HelloWord executable file just generated, and the running result is the same as the above running result.
So far, through a few simple clang commands, you can compile and run a simple Objective-C code of HelloWord.
Link to the original text: http://www.jianshu.com/p/c2dabb4fd761
The copyright belongs to the author. For reprinting, please contact the author for authorization and mark "the author of the short book".