当前位置: 首页 > 工具软件 > CPython > 使用案例 >

Linux下CPython源码的编译

汪深
2023-12-01

最近在看一本python的书CPython Internals,打算研究一下CPython的代码,然后就在Linux(ubuntu)下编译了CPython源码,在此记录一下。

1. 使用git下载cpython源码

git clone --branch 3.9 https://github.com/python/cpython
cd cpython

2. 使用weget下载cpython3.9.7版本,使用这种是适用于没安装git或者访问github网络比较差的小伙伴

 wget https://github.com/python/cpython/archive/refs/tags/v3.9.7.tar.gz
 tar zxf v3.9.7.tar.gz
 cd cpython-3.9.7

3. 下载完成之后,需要对cpython进行编译

# 编译工具安装
sudo apt install build-essential
# 依赖库安装
sudo apt install libssl-dev zlib1g-dev libncurses5-dev libncursesw5-dev libreadline-dev libsqlite3-dev libgdbm-dev libdb5.3-dev libbz2-dev libexpat1-dev liblzma-dev libffi-dev

4. 运行配置脚本,配置的时候选择一个debug模式,也可以不选择,这个看个人选择。 

./configure --with-pydebug

5. 生成Makefile,j2表示启动两个job来加速编译,-s 表示silent,即不打印output

 make -j2 -s

注:在linux执行make --help可参考具体的参数选项

mark123@ubuntu-vm:~/cpythonCode/cpython$ make --help
Usage: make [options] [target] ...
Options:
  -b, -m                      Ignored for compatibility.
  -B, --always-make           Unconditionally make all targets.
  -C DIRECTORY, --directory=DIRECTORY
                              Change to DIRECTORY before doing anything.
  -d                          Print lots of debugging information.
  --debug[=FLAGS]             Print various types of debugging information.
  -e, --environment-overrides
                              Environment variables override makefiles.
  --eval=STRING               Evaluate STRING as a makefile statement.
  -f FILE, --file=FILE, --makefile=FILE
                              Read FILE as a makefile.
  -h, --help                  Print this message and exit.
  -i, --ignore-errors         Ignore errors from recipes.
  -I DIRECTORY, --include-dir=DIRECTORY
                              Search DIRECTORY for included makefiles.
  -j [N], --jobs[=N]          Allow N jobs at once; infinite jobs with no arg.
  -k, --keep-going            Keep going when some targets can't be made.
  -l [N], --load-average[=N], --max-load[=N]
                              Don't start multiple jobs unless load is below N.
  -L, --check-symlink-times   Use the latest mtime between symlinks and target.
  -n, --just-print, --dry-run, --recon
                              Don't actually run any recipe; just print them.
  -o FILE, --old-file=FILE, --assume-old=FILE
                              Consider FILE to be very old and don't remake it.
  -O[TYPE], --output-sync[=TYPE]
                              Synchronize output of parallel jobs by TYPE.
  -p, --print-data-base       Print make's internal database.
  -q, --question              Run no recipe; exit status says if up to date.
  -r, --no-builtin-rules      Disable the built-in implicit rules.
  -R, --no-builtin-variables  Disable the built-in variable settings.
  -s, --silent, --quiet       Don't echo recipes.
  -S, --no-keep-going, --stop
                              Turns off -k.
  -t, --touch                 Touch targets instead of remaking them.
  --trace                     Print tracing information.
  -v, --version               Print the version number of make and exit.
  -w, --print-directory       Print the current directory.
  --no-print-directory        Turn off -w, even if it was turned on implicitly.
  -W FILE, --what-if=FILE, --new-file=FILE, --assume-new=FILE
                              Consider FILE to be infinitely new.
  --warn-undefined-variables  Warn when an undefined variable is referenced.

This program built for x86_64-pc-linux-gnu
Report bugs to <bug-make@gnu.org>
mark123@ubuntu-vm:~/cpythonCode/cpython$

6. 执行./python 进入python交互式环境

mark123@ubuntu-vm:~/cpythonCode/cpython$ ./python
Python 3.9.7 (default, Sep 11 2021, 15:23:18)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>

至此,CPython源码已经编译完成。

 类似资料: