# codex基础部分

# 安装wsl2

# 打开虚拟化平台

右击开始图标,选择终端管理员,然后输入以下命令,然后重启电脑

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

# 下载wsl2

wsl --install

记住这里记得关闭代理,否则会下载失败

我这里一直安装失败,显示安全频道支持出错,因此前往[github](Releases · microsoft/WSL)下载了对应版本,然后双击安装即可。

# 设置wsl版本

wsl --set-default-version 2

# wsl运行ubuntu系统

我这里选择的是ubuntu22.04,这个系统有两种方法下载

  1. 去微软商店搜索并下载(速度较慢但是方便)
  2. [网站下载](Manual installation steps for older versions of WSL | Microsoft Learn)(建议复制相应版本的下载链接然后用迅雷下载,然后双击安装即可)

安装完成之后,按照提示输入用户名和密码。

# 磁盘迁移(可选)

wsl默认会把ubuntu虚拟磁盘放在C盘,路径大概是:

C:\Users\你的用户名\AppData\Local\Packages\Canonical...\LocalState\ext4.vhdx

我这边是将其迁移至D盘,D盘的存储空间更大,减少存储焦虑

  1. 关闭wsl

    还是用管理员终端执行

    wsl --shutdown
  2. 导出当前ubuntu

    假如你想放在D:\wsl\Ubuntu,则去D盘创建一个wsl文件夹,然后导出:

    wsl --export Ubuntu D:\WSL\ubuntu.tar
  3. 注销当前C盘的ubuntu

    wsl --unregister Ubuntu
  4. 从D盘重新导入

    wsl --import Ubuntu D:\WSL\Ubuntu D:\WSL\ubuntu.tar --version 2
  5. 设置默认用户

    重新进入ubuntu:

    wsl -d Ubuntu

    可能默认会进入root用户,设置默认用户:

    sudo nano /etc/wsl.conf

    写入:

    [user]
    default=xy

    xy则是我刚刚创建的用户名,根据需求修改即可,然后保存(ctrl+x,y,回车),然后退出

    重启wsl

    wsl --shutdown

    再进入:

    wsl

    应该就回到你的用户了,然后去相应文件夹查看是否有ext4.vhdx,

    D:\WSL\Ubuntu\ext4.vhdx

    如果有则说明成功。

# codex cli的安装

  1. 安装Node.js

    sudo apt update
    sudo apt install -y ca-certificates curl gnupg
    curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
    sudo apt install -y nodejs
  2. 验证

    node -v
    npm -v
    which node
    which npm
  3. 创建全局目录

    mkdir -p ~/.npm-global
  4. 让npm用这个目录

    npm config set prefix '~/.npm-global'
  5. 把它加入到path

    echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.bashrc
    source ~/.bashrc

    检查

    npm config get prefix
    echo $PATH | tr ':' '\n' | head
  6. 安装codex cli

    npm install -g @openai/codex

    验证

    codex --version
    which codex
  7. 登录codex

    codex login

    如果使用代理,记得使用tun模式,否在会显示token exchanged failed: token endpoint returned status 403 Forbidden

# vscode连接wsl

# 正常安装vscode,无脑下一步即可

# 安装wsl插件:

Snipaste_2026-02-25_11-24-27.png

# 在wsl打开vscode

cd ~
mkdir -p ~/codex_project
cd codex_project
code .

第一次可能会提示安装 “VS Code Server”(自动完成即可)。
这时 VS Code 左下角会显示 WSL: Ubuntu,说明你已经“在 WSL 里开发”了。

然后,按下 **ctrl+`**打开终端,然后输入pwd,应该显示对应路径及说明连接成功,如图所示

1771990830787.png

# 配置git

# 安装git

在 VS Code 的 WSL 终端里运行:

git --version

如果没装:

sudo apt update
sudo apt install -y git

# 设置全局用户名和邮箱

这个是提交记录里显示的身份。

git config --global user.name "xiaoyu"
git config --global user.email "your_email@example.com"

(邮箱建议用 GitHub 注册邮箱)

检查:

git config --global --list

# 调整默认分支

git config --global init.defaultBranch main

# 生成ssh key(用于github推送)

这一步非常重要。

  1. 生成密钥
ssh-keygen -t ed25519 -C "your_email@example.com"

一路回车即可。

  1. 启动 ssh-agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
  1. 查看公钥
cat ~/.ssh/id_ed25519.pub

复制输出内容。

  1. 添加到 GitHub

GitHub → Settings → SSH and GPG Keys → New SSH Key
粘贴进去。

  1. 测试连接
ssh -T git@github.com

如果成功会看到:

Hi username! You've successfully authenticated...

若失败,有可能是代理问题,关闭代理即可

# codex cli的简单使用

直接在vscode打开的终端进行简单测试

  1. 提问问题

    codex "冒泡排序的时间复杂度是多少"

    那么他就会进行回答

     冒泡排序的时间复杂度:平均和最坏为 O(n^2),最好(已排序且有提前退出优化)为 O(n)


2. 读取文件并返回结果

   这边写了使用另一个错误的算法,让codex帮忙找出来错误,并请举例证明,然后并修改代码

   ```cpp
   // 错误代码
   #include <iostream>
   #include <vector>
   using namespace std;
   
   int binarySearch(vector<int>& nums, int target) {
       int left = 0;
       int right = nums.size() - 1;
   
       while (left < right) {   
           int mid = (left + right) / 2;
   
           if (nums[mid] < target)
               left = mid;
           else
               right = mid - 1;
       }
   
       if (nums[left] == target)
           return left;
   
       return -1;
   }
   
   int main() {
       vector<int> nums = {1, 3};
       int target = 3;
   
       int index = binarySearch(nums, target);
       cout << "Index: " << index << endl;
   
       return 0;
   }

使用以下语句

codex "请分析当前目录下的 test01.cpp 文件。
找出 binarySearch 函数中的逻辑错误。
请举一个具体的错误示例(长度大于5)来证明该算法有问题。
然后给出修改后的正确代码。"

下面是运行结果

 我会先读取 test01.cpp,定位 binarySearch 的实现并复现它的错误路径,然后给出一个长度大于 5 的具体反例和修正后的代码。

 我会先查看 test01.cpp 的实现,定位 binarySearch 的具体逻辑错误,然后给出一个长度大于 5 的反例并提供修正后的代码。先读取文件内容。

 Explored
 List ls -la
    Read test01.cpp

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 已经定位到核心问题:left = mid while (left < right) 的组合会在某些情况下导致死循环,同时 right = mid - 1 还会跳过潜在答案。接下来我会直接把 binarySearch 改成标准的闭区间写法并给出反例说明。

 Edited test01.cpp (+6 -7)
     8
     9 -    while (left < right) {
     9 +    while (left <= right) {
    11
    12 -        if (nums[mid] < target)
    13 -            left = mid;
    12 +        if (nums[mid] == target)
    13 +            return mid;
    14 +        else if (nums[mid] < target)
    15 +            left = mid + 1;
    16          else

    18      }
    17 -
    18 -    if (nums[left] == target)
    19 -        return left;
    19

    30      return 0;
    32 -}
    31 +}

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

 我已完成代码修正,接下来会快速编译运行一次,确认函数行为正常。

 Ran g++ -std=c++17 test01.cpp -o test01 && ./test01
 /bin/bash: line 1: g++: command not found

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

 编译器在当前环境里不可用(g++: command not found),我会基于源码给出完整错误路径分析、具体反例和修正代码。

 binarySearch 的主要逻辑错误有两个:

  1. left = mid 可能导致死循环
 left + 1 == right nums[mid] < target 时,mid == left,更新后 left 不变,循环无法推进。
  2. while (left < right) + right = mid - 1 会漏掉候选位置
     这种写法在边界处理上不一致,容易把仍可能是答案的位置提前排除。

  具体位置见:test01.cpp (/home/xy/codex_project/test01.cpp)

  ———

  错误示例(长度 > 5):

  - 数组:[1, 2, 3, 4, 5, 6](长度 6)
  - 目标:6

  按原代码执行:

  1. left=0, right=5, mid=2, nums[2]=3<6 -> left=2
  2. left=2, right=5, mid=3, nums[3]=4<6 -> left=3
  3. left=3, right=5, mid=4, nums[4]=5<6 -> left=4
  4. left=4, right=5, mid=4, nums[4]=5<6 -> left=4(不变)
  5. 之后一直重复第 4 步,死循环。

  ———

  修改后的正确代码(已更新到文件):

  int binarySearch(vector<int>& nums, int target) {
      int left = 0;
      int right = nums.size() - 1;

      while (left <= right) {
          int mid = (left + right) / 2;

          if (nums[mid] == target)
              return mid;
          else if (nums[mid] < target)
              left = mid + 1;
          else
              right = mid - 1;
      }

      return -1;
  }

  补充:我尝试编译验证,但当前环境没有 g++(g++: command not found)。

修改后的代码如下:

#include <iostream>
#include <vector>
using namespace std;

int binarySearch(vector<int>& nums, int target) {
    int left = 0;
    int right = nums.size() - 1;

    while (left <= right) {   
        int mid = (left + right) / 2;

        if (nums[mid] == target)
            return mid;
        else if (nums[mid] < target)
            left = mid + 1;
        else
            right = mid - 1;
    }

    return -1;
}

int main() {
    vector<int> nums = {1, 3};
    int target = 3;

    int index = binarySearch(nums, target);
    cout << "Index: " << index << endl;

    return 0;
}

然而我们发现了,我们没有g++环境,因此我说了


codex "帮我下载g++"

以下是运行结果

 I'm running sudo apt-get update and sudo apt-get install -y g++ to install the necessary C++ compiler.

• Ran sudo apt-get update
  └ sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper

• I'm testing running the command with a tty to allow interactive password prompt for sudo escalation, aiming to handle cases needing user input securely.

 Planning to prompt the user to enter or paste the password and pass it via write_stdin to the session for authentication.

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

 需要 sudo 密码才能继续安装。请把密码发给我(只会用于这次 sudo apt-get update),我会继续完成 g++ 安装。




 Interacted with background terminal · sudo apt-get update
 123456

 Interacted with background terminal · sudo apt-get update
 (waited)

 Ran sudo apt-get update

Hit:1 https://deb.nodesource.com/node_22.x nodistro InRelease       
 +26 lines
Reading package lists... 99%
Reading package lists... Done

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

 还需要一次 sudo 密码来安装 g++。请再次输入密码。



 Interacted with background terminal · sudo apt-get install -y g++
 123456

 Interacted with background terminal · sudo apt-get install -y g++
 (waited)

 Ran sudo apt-get install -y g++

Reading package lists... Done
 +205 lines
    Processing triggers for man-db (2.10.2-1) ...
    Processing triggers for libc-bin (2.35-0ubuntu3.8) ...

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

 已安装完成。
  你可以用下面命令确认版本:

  g++ --version

  需要我顺便装 build-essential 或配置默认编译选项吗?


 g++ --version


 Ran g++ --version
 g++ (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0
    Copyright (C) 2021 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

 结果如下:

  g++ (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0

  需要我帮你安装更高版本(比如 g++-12 g++-13)并切换默认吗?

然后再次让codex帮我们检查代码

codex "那么现在能否帮我编译且运行test01.cpp,帮我检查他是否正确了呢?"

结果如下

 codex "那么现在能否帮我编译且运行test01.cpp,帮我检查他是否正确了呢?"
╭─────────────────────────────────────────────╮
 >_ OpenAI Codex (v0.104.0)
 model:     gpt-5.3-codex   /model to change
 directory: ~/codex_project
╰─────────────────────────────────────────────╯
 那么现在能否帮我编译且运行test01.cpp,帮我检查他是否正确了呢?


 我会先在当前项目里定位 test01.cpp,然后用合适的编译命令编译并直接运行,把编译错误或运行结果完整检查后告诉你结论。

 Ran pwd; ls -la; rg --files | rg 'test01\.cpp$'
 /home/xy/codex_project
    total 12
 +2 lines
    -rw-r--r-- 1 xy xy  587 Feb 25 17:59 test01.cpp
    test01.cpp

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

 我已经找到文件,下一步直接用 g++ 按常用标准编译并执行,检查是否有报错、警告以及输出是否符合预期。

 Ran g++ -std=c++17 -Wall -Wextra -pedantic test01.cpp -o test01 && ./test01
 Index: 1

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

 程序已成功运行,我再快速看一下 test01.cpp 代码本身,确认这个输出和实现逻辑是否一致。

 Explored
 Read test01.cpp

 Worked for 1m 43s ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

 可以,已经帮你编译并运行了,结果如下:

  - 编译命令:g++ -std=c++17 -Wall -Wextra -pedantic test01.cpp -o test01
  - 运行输出:Index: 1
  - 编译通过且无警告,程序可正常运行。

  按你当前这组测试数据(nums = {1, 3}, target = 3),结果是正确的。
  代码在这里:test01.cpp (/home/xy/codex_project/test01.cpp)

# codex小补充

注意切换成root用户可能会导致codex需要重新登录,按照以下操作即可避免二次登录

  1. 切换root用户
  2. 创建相应文件夹
  3. 复制文件,其中xy则是你的ubuntu用户名
sudo -i
mkdir -p /root/.codex
cp /home/xy/.codex/auth.json /root/.codex/