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

CommandDescription
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

OptionDescription
--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允许在 arcorc 模式下使用 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输出特定警告 XX 可为 all
--warningAsError:X:on|off将警告 X 视为错误
--skipCfg:on|off不读取 Nim 安装目录的配置文件
--skipUserCfg:on|off不读取用户配置文件 ~/.config/nim/config.nims
--skipProjCfg:on|off不读取项目配置文件

Nimble

CommandDescription
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

OptionDescription
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
0 views
Back to Blog

Related posts

Read more »

PQP Language

Overview Name: PQP Language Description: It is a mini programming language created to demonstrate how the process of building a language works. !pichttps://med...