> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/di-41-zhang-kai-fa-gong-ju/di-41.1-jie-code-server-he-clangd-kai-fa-huan-jing.md).

# 41.1 code-server 和 clangd 开发环境

code-server 是 Visual Studio Code 的开源服务器版本，可在远程服务器上运行并通过浏览器访问。

## 安装 code-server

使用 pkg 安装：

```sh
# pkg install code-server
```

使用 Ports 安装：

```sh
# cd /usr/ports/www/code-server/ 
# make install clean
```

启用服务：

```sh
# sysrc code_server_enable=YES
# service code-server start
```

在浏览器中访问 `http://127.0.0.1:8080` 即可使用该服务。

> **警告**
>
> 建议不要在未配置认证/HTTPS 时直接对外网或局域网开放该端口。

## 服务器安装 LLVM 与 clangd 插件

在 FreeBSD 上本地安装 LLVM 工具链，并配置 code-server：

```sh
# pkg install -y llvm # 安装 LLVM 工具链
# code-server --install-extension llvm-vs-code-extensions.vscode-clangd   # 在 code-server 中安装 clangd 扩展
```

## （示例）在浏览器中用 code-server 打开 FreeBSD 的源代码树

使用 code-server 打开 **/usr/src** 目录作为工作区：

```sh
# code-server /usr/src
```

## （示例）在浏览器中编译最小化内核并生成 `compile_commands.json` 文件

安装 Bear 工具并生成内核构建的编译命令数据库：

```sh
# pkg install bear   # 安装 Bear 工具，用于生成 compile_commands.json
# bear --append -- make KERNCONF=MINIMAL buildkernel   # 使用 Bear 生成内核构建的编译命令，并构建 MINIMAL 内核
```

等待编译完成并生成 `compile_commands.json` 文件后，即可开始阅读内核关键部分的源代码。

## 故障排除与未竞事宜

### HTTPS 配置

本节未涉及如何在服务器上通过 HTTPS 提供 code-server 服务。如需配置 HTTPS，可参考 code-server 官方文档配置 SSL 证书。


---

# 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://book.bsdcn.org/di-41-zhang-kai-fa-gong-ju/di-41.1-jie-code-server-he-clangd-kai-fa-huan-jing.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.
