python setup.py的使用

查看python setup.py相关命令

主要分成build和install两类
python setup.py --help-commands

hui@hui-XPS-8920:~$ python spam_setup.py --help-commands
Standard commands:
  build            build everything needed to install
  build_py         "build" pure Python modules (copy to build directory)
  build_ext        build C/C++ extensions (compile/link to build directory)
  build_clib       build C/C++ libraries used by Python extensions
  build_scripts    "build" scripts (copy and fixup #! line)
  clean            clean up temporary files from 'build' command
  install          install everything from build directory
  install_lib      install all Python modules (extensions and pure Python)
  install_headers  install C/C++ header files
  install_scripts  install scripts (Python or otherwise)
  install_data     install data files
  sdist            create a source distribution (tarball, zip file, etc.)
  register         register the distribution with the Python package index
  bdist            create a built (binary) distribution
  bdist_dumb       create a "dumb" built distribution
  bdist_rpm        create an RPM distribution
  bdist_wininst    create an executable installer for MS Windows
  check            perform some checks on the package
  upload           upload binary package to PyPI

usage: spam_setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: spam_setup.py --help [cmd1 cmd2 ...]
   or: spam_setup.py --help-commands
   or: spam_setup.py cmd --help

如下为python编译C extensions的步骤

# 编译
# python setup.py build --inplace 
python setup.py build_ext --inplace
# 清楚.o的临时文件
python setup.py clean
# 在python的lib目录下建立软连接到编译生成的.so
python setup.py install

python spam_setup.py build_ext

生成了build目录,里面有两个文件夹lib.xxxx和temp.xxxx,lib.xxxx里面是编译生成的.so动态库,temp.xxxx是编译临时生成的二进制文件.

hui@hui-XPS-8920:~$ python setup.py build_ext
running build_ext
building 'spam' extension
creating build
creating build/temp.linux-x86_64-3.7
gcc -pthread -B /home/hui/ide/miniconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/hui/ide/miniconda3/include/python3.7m -c spam.c -o build/temp.linux-x86_64-3.7/spam.o
creating build/lib.linux-x86_64-3.7
gcc -pthread -shared -B /home/hui/ide/miniconda3/compiler_compat -L/home/hui/ide/miniconda3/lib -Wl,-rpath=/home/hui/ide/miniconda3/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.7/spam.o -o build/lib.linux-x86_64-3.7/spam.cpython-37m-x86_64-linux-gnu.so
hui@hui-XPS-8920:~$ ls -R
.:
build  setup.py  spam.c

./build:
lib.linux-x86_64-3.7  temp.linux-x86_64-3.7

./build/lib.linux-x86_64-3.7:
spam.cpython-37m-x86_64-linux-gnu.so

./build/temp.linux-x86_64-3.7:
spam.o

python setup.py build_ext --inplace

相对python setup.py build_ext,会把.so库文件生成在当前目录下,而不会生成一个lib.xxx目录放置该文件

hui@hui-XPS-8920:~$ python setup.py build_ext --inplace
running build_ext
building 'spam' extension
creating build
creating build/temp.linux-x86_64-3.7
gcc -pthread -B /home/hui/ide/miniconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/hui/ide/miniconda3/include/python3.7m -c spam.c -o build/temp.linux-x86_64-3.7/spam.o
gcc -pthread -shared -B /home/hui/ide/miniconda3/compiler_compat -L/home/hui/ide/miniconda3/lib -Wl,-rpath=/home/hui/ide/miniconda3/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.7/spam.o -o /home/data/github/BlogCode/python_invoke_cpp/python_C_API/spam.cpython-37m-x86_64-linux-gnu.so
hui@hui-XPS-8920:~$ ls -R
.:
build  setup.py  spam.c  spam.cpython-37m-x86_64-linux-gnu.so

./build:
temp.linux-x86_64-3.7

./build/temp.linux-x86_64-3.7:
spam.o

python setup.py clean

删除编译生成的.o临时文件,如果build下删空了,则删除build,不会删除.so

hui@hui-XPS-8920:~/github/BlogCode/python_invoke_cpp/python_C_API$ python setup.py clean
running clean
removing 'build/temp.linux-x86_64-3.7' (and everything under it)
removing 'build'
hui@hui-XPS-8920:~/github/BlogCode/python_invoke_cpp/python_C_API$ ls
setup.py  spam.c  spam.cpython-37m-x86_64-linux-gnu.so

你可能感兴趣的:(python,cpp混合编程,python,extensions,c++)