Complex systems come into existence in only two ways: through careful planning or through evolution. An airport is an example of something that is planned carefully beforehand, built, and then undergoes only minor changes for the rest of its existence. Complex organisms (like humans) are an example of something that has evolved continually from something simple. In the end, organisms that are well suited to evolution will always win out over organisms that are less suited to evolve.
Mac OS X同时提供了gcc和clang, 需要跨平台,兼容性选gcc, 只在苹果平台直接选clang
Handy Flags
-g: Add debugging symbols.
-E: See preprocessor output.
-S: See generated assembly code. (as)
-save-temps: Keep temporary files around.
-Wall, -Wmost: Show more warning.
-Werror: Treat warnings as errors.
DSYMBOL: #define from the command line.
DSYMOBL=value
-0#: Set optimization levels.
-std: Choose a standard, or non-standard, C dialect. Supplying -std=c99 will give you C99. 除了特定几个gcc extensions和c++特性以外全部关闭,你可以通过设置-std=gun99来兼容C99和gcc's extensions.
Debugging
-g flag 添加debug segment, 通过strip (man: remove symbols) 指令可以从程序中移除
htonl(): host to network long, ntohl(): network to host long
Chapter 15: CFRunLoop
A run loop is an event loop in a thread that looks for events, such as mouse clicks, key presses, and timer firings. The run loop dispatches these events to interested parties and then goes to sleep waiting for more events.
Mac OS X: Built to Evolve
几个概念:
Chapter 1: C and Objective-C
The Compiler pipeline: Preprocess - Compile - Link
The C preprocessor
% gcc -E -dM - < /dev/null
可以查看编译器预定义的符号:注: 多达3, 400个, -E flag显示p reprocess output, -dM: debugging flags, < /dev/null 给一个空的输入
Objective-C
在C的基础上扩展了面向对象的语法,同时加多了一套Foundation的框架,比如NSString, NSNUmber, NSArray, NS everything。
Chapter 2: The Compiler
Handy Flags
Debugging
Warnings
app.c
clang app.c
没有warningsclang -Wall app.c
, warning all这是就会抱怨说I没有使用,warning: unused variable 'I'
clang -Wall -Wno-unused app.c
64-bit
#include <sys/types.h>
中这些类型都是64-bit (long),所以在int或uint32_t转换时需要注意溢出Chapter 3: Blocks
app.c
在Mac OS上gcc/clang都可以编译运行,但是在linux/ubuntu上的gcc就不行。我理解就是如果你写macOS C就可以直接使用Objective-C中的特性,只需要知道放弃移植性,取舍问题而已。
Chapter 4: Command-Line Programs
app.m
运行后交互如下:
Looking at the Environment
getenv
可以获取到环境变量,调用时shell不管是在.zshrch还是临时都需要通过export FOO=BAR
进行设置即可。export
仅限于本次session,包括从这个session fork出来的childrenexport -p
打印此session所有环境变量为什么需要export?
shell - What do the bash-builtins 'set' and 'export' do? - Unix & Linux Stack Exchange
Chapter 5: Exceptions, Error Handling, and Signals
With the Unix API, 两个方式告知你错误:
errno
注: hostent, noent中的ent都是entry的缩写。
signals
Exception Handling in Cocoa
通过NS_DURING, NS_HANDLER来模拟try...catch
SIGSEGV
注: 捕捉null pointer 产生的signal,然后通过jmp来恢复。
Chapter 6: Library
一般情况自己直接用static library就行。
Chapter 7: Memory
栈上分配的数据会在函数结束时回收,所以不应该返回stack的内存指针,会产生错误。
如果需要超越stack, 那么就需要用heap, 通过malloc/free/realloc/reallocf操作heap。
发现memory leak:
Chapter 8: Debugging With GDB
Chapter 9: DTrace
Chapter 10: Performance Tuning
// skip, 留给二刷
Chapter 11: Files, Part 1: I/O and Permissions
Chapter 12: Files, Part 2: Directories, File Systems, and Links
Chapter 13: NSFileManager - Cocoa and the File System
默认I/O操作是同步的,异步需要采用select/poll模型。
Formatted I/O
/Users/markd/Documents/Badgers.acorn
Chapter 14: Network Programming With Sockets
sockaddr
sockaddr_in的_in后缀表示Internet,sockeaddr用IPv4的结构,ip地址,端口来解释sockaddr中的14个字节, 所以内存是mapping的,只是解释不同而已。sin_port, sin_addr, sin_zero加起来共14个字节。
Network Byte Order
little-endian: 0x78 56 34 12
big-endian: 0x12 34 56 78
uint16_t htons (uint16_t hostshort);
uint32_t htonl(uint32_t hostlong);
uint16_t ntohs (uint16_t netshort);
uint32_t ntohl (uint32_t netlong);
注:
Chapter 15: CFRunLoop
Core Foundation通过CFRunLoop, CFSocket和CFHost提升了thread, socket的层次。
Chapter 17: Bonjour
Bonjour采用mDNS,所以只能在local network中,格式为name.type.domain, 如_home-sharing._tcp.local, 你可以publish你自己的服务,比如_chat._tcp.local,然后等待其他客户端来发现。
可以指定type查找
dns-sd -B _home-sharing._tcp
Chapter 18: Multiprocessing
Chapter 19: Using NSTask
Chapter 20: Multithreading
Chapter 21: Operations
Chapter 22: Grand Central Dispatch
Chapter 23: Accessing the Keychain
这部分讲的比较简单, 没讲透。
扩展