> For the complete documentation index, see [llms.txt](https://koshizuow.gitbook.io/compilerbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://koshizuow.gitbook.io/compilerbook/separate_compile_linking.md).

# 分離編譯與連結

至今為止，我們各用了一個C程式和 shell 腳本檔案的架構來進行開發。這樣的架構也沒有什麼問題，但是漸漸程式碼變得愈來愈長，差不多可以把C的部份分割成複數個檔案來改善程式的閱讀性了。在這一步，我們要把 9cc.c 這個檔案分割成以下5個檔案：

* `9cc.h`: 標頭檔
* `main.c`: `main`函式
* `parse.c`: 分析器
* `codegen.c`: 指令產生器
* `container.c`: 向量（vector）、對應（map），與所對應的測試程式碼

`main`函式很小，可以塞進別的C程式檔裡，但是從意義上來看並不屬於 parse.c 或 codegen.c 的任一個，所以獨立為1個檔案。

本章會介紹分離編譯的概念並說明其意義，然後會說明具體執行的步驟。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://koshizuow.gitbook.io/compilerbook/separate_compile_linking.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
