Nim
Published: (February 9, 2026 at 02:31 AM EST)
5 min read
Source: Dev.to
Source: Dev.to
Configuration File
# ~/.config/nim/config.nims
import std/[strutils, strformat]
switch("nimcache", fmt"{getCurrentDir()}/nimcache/{projectName()}/{CompileTime.toHex}")
switch("colors", "on")
# switch("usenimcache") # 设置后无法正常使用testament
# switch("backend", "c") # 设置后 cc, cpp, js 命令失效
Compilation Commands
# 编译 main.nim
nim c main.nim
# 编译并运行 main.nim
nim r main.nim
nim c -r main.nim
# 编译项目入口
nim c -o:"$projectname" --outdir:"$nimcache" ./src/project.nim
Nim Command Syntax
nim command [options] [projectfile] [arguments]
Primary Commands
| Command | Description |
|---|---|
c (compile) | 编译 |
r | 编译并运行 |
doc | 生成文档 |
cc (compileToC) | 以 C 编译 |
cpp (compileToCpp) | 以 C++ 编译 |
js | 以 JavaScript 编译 |
-r / --run | 执行编译后的程序 |
-d:SYMBOL / --define:SYMBOL | 设置编译器符号 |
Common Symbols
-d:release– 发布模式-d:testing– 测试模式-d:ssl– 激活 SSL
General Options
| Option | Description |
|---|---|
--app:console|gui|lib|staticlib | 编译程序类型:控制台、GUI、动态库、静态库 |
-p:PATH / --path:PATH | 增加搜索路径 |
--backend:c|cpp|js|objc | 设置编译后端 |
--noMain:on|off | 不生成 proc main |
--nimMainPrefix:PREFIX | 跨后端整合时配置初始化函数前缀,形成 PREFIXNimMain() |
-o:FILE / --out:FILE | 设置输出文件名称 |
--outdir:DIR | 设置输出文件位置(在使用 -o 后生效) |
--usenimcache | 自动将 outdir 设为 nimcache(在使用 -o 时无效) |
--nimcache:PATH | 设置 nimcache 目录 |
--mm:orc|arc|refc|markAndSweep|boehm|go|none|regions | 配置内存管理方式 |
--deepcopy:on|off | 允许在 arc、orc 模式下使用 system.deepCopy |
--threads:on|off | 是否开启多线程 |
--colors:on|off | 是否开启彩色信息 |
--stackTrace:on|off | 是否开启堆栈追踪 |
--lineTrace:on|off | 是否开启行追踪 |
--debuginfo:on|off | 是否输出调试信息 |
--passC:OPTION | 传递给 C 编译器的选项 |
--passL:OPTION | 传递给链接器的选项 |
-c / --compileOnly:on|off | 只编译,不汇编、链接 |
--noLinking:on|off | 只编译、汇编,不链接 |
--putenv:key=value | 设置环境变量 |
-w:on|off|list / --warnings:on|off|list | 是否输出警告 |
--warning:X:on|off | 输出特定警告 X(X 可为 all) |
--warningAsError:X:on|off | 将警告 X 视为错误 |
--skipCfg:on|off | 不读取 Nim 安装目录的配置文件 |
--skipUserCfg:on|off | 不读取用户配置文件 ~/.config/nim/config.nims |
--skipProjCfg:on|off | 不读取项目配置文件 |
Nimble
| Command | Description |
|---|---|
nimble init PROJECT | 初始化 Nim 项目 |
nimble install [DEPENDENCE] | 安装 Nim 依赖 |
nimble uninstall DEPENDENCE | 删除 Nim 依赖(不能作为其他依赖的依赖) |
nimble list | 查看全局安装的依赖 |
nimble list -i / --installed | 查看本地安装的依赖 |
nimble deps | 以树结构展示依赖信息 |
nimble deps --format:json | 以 JSON 结构展示依赖 |
nimble test | 执行 .nimble 中配置的测试 |
nimble build | 按默认配置或 $projectdir/config.nims 构建 |
testament p "tests/test.nim" | 运行指定测试文件 |
Testament
discard """
action: "run"
targets: "c"
cmd: "nim r -d:testing $file"
"""
Usage
testament command [projectfile] [options]
Options
| Option | Description |
|---|---|
p PATTERN, pat PATTERN, pattern PATTERN | 测试路径符合 PATTERN 的文件 |
all | 测试所有位于 tests/category 下的 .nim 文件 |
r SINGLE, run SINGLE | 测试 tests/category 下的单个文件 SINGLE(.nim) |
--targets:c|cpp|js|objc | 设置编译后端 |
--colors:on|off | 是否开启彩色信息 |
Example Commands
# 测试单个文件
testament p "tests/test.nim"
# 测试通配符
testament p "tests/*.nim"
# 运行单个测试
testament r "example.nim"
Default Test Invocation
nim $target --hints:on -d:testing --nimblePath:build/deps/pkgs $options $file
$target取值为c,cpp,js,objc(可用空格分隔多个后端)matrix用于在测试时传递多个-d:SYMBOL,使用分号分隔,例如; -d:release; -d:caseFoo -d:release