# ros2启动：替换错误：启动文件中未定义变量'my_var'

- **ID:** `ros2/launch-file-substitution-undefined-variable`
- **领域:** ros2
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

启动文件使用了未声明或未作为参数传递的变量替换（例如$(var my_var)），导致解析失败。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Humble | active | — | — |
| Iron | active | — | — |
| Jazzy | active | — | — |
| Rolling | active | — | — |

## 解决方案

1. ```
   Add a DeclareLaunchArgument for the variable in the launch file.
   ```
2. ```
   Pass the variable as a command-line argument when launching.
   ```
3. ```
   Use a default value in the substitution to avoid undefined errors.
   ```

## 无效尝试

- **Using $(env my_var) instead of $(var my_var) without setting environment variable** — Substitution type mismatch; $(env) expects an environment variable, not a launch argument. (60% 失败率)
- **Adding the variable directly in the launch file without DeclareLaunchArgument** — ROS2 launch requires explicit declaration; inline usage without declaration still fails. (70% 失败率)
- **Setting the variable in a separate YAML config file without including it** — YAML config files aren't automatically loaded; they must be included via IncludeLaunchDescription. (50% 失败率)
