python-config / python3-config - output build options for python C/C++ extensions or embedding

本文详细介绍了使用python-config和python3-config工具来获取Python C/C++扩展或嵌入式编译选项的方法。包括如何获取编译器标志、链接器标志、包含路径、库路径、前缀目录、执行前缀目录、扩展模块后缀和配置目录等关键信息。

python-config / python3-config - output build options for python C/C++ extensions or embedding

1. NAME
python-config - output build options for python C/C++ extensions or embedding

embed [ɪm'bed; em-]:vt. 栽种,使嵌入,使插入,使深留脑中

2. SYNOPSIS
python-config [ --prefix ] [ --exec-prefix ] [ --includes ] [ --libs ] [ --cflags ] [ --ldflags ] [ --extension-suffix ] [ --configdir ] [ --help ]

3. DESCRIPTION
python-config helps compiling and linking programs, which embed the Python interpreter, or extension modules that can be loaded dynamically (at run time) into the interpreter.

extension [ɪk'stenʃ(ə)n; ek-]:n. 延长,延期,扩大,伸展,电话分机

4. OPTIONS
--cflags
print the C compiler flags.

python-config --cflags
python3-config --cflags

--ldflags
print the flags that should be passed to the linker.

python-config --ldflags
python3-config --ldflags

--includes
similar to --cflags but only with -I options (path to python header files).

python-config --includes
python3-config --includes

--libs
similar to --ldflags but only with -l options (used libraries).

python-config --libs
python3-config --libs

--prefix
prints the prefix (base directory) under which python can be found.

python-config --prefix
python3-config --prefix

--exec-prefix
print the prefix used for executable program directories (such as bin, sbin, etc).

python-config --exec-prefix
python3-config --exec-prefix

--extension-suffix
print suffix used for extension modules (including the _d modified for debug builds).

suffix ['sʌfɪks]:vt. 添后缀 n. 后缀,下标
python-config --extension-suffix
python3-config --extension-suffix

--configdir
prints the path to the configuration directory under which the Makefile, etc. can be found).

python-config --configdir
python3-config --configdir

--help
print the usage message.

python-config --help
python3-config --help

5. python-config

strong@foreverstrong:~$ python-config --cflags
-I/usr/include/python2.7 -I/usr/include/x86_64-linux-gnu/python2.7  -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python-config --ldflags
-L/usr/lib/python2.7/config-x86_64-linux-gnu -L/usr/lib -lpython2.7 -lpthread -ldl  -lutil -lm  -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python-config --includes
-I/usr/include/python2.7 -I/usr/include/x86_64-linux-gnu/python2.7
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python-config --libs
-lpython2.7 -lpthread -ldl  -lutil -lm 
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python-config --prefix
/usr
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python-config --exec-prefix
/usr
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python-config --extension-suffix
.so
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python-config --configdir
/usr/lib/python2.7/config-x86_64-linux-gnu
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python-config --help
Usage: /usr/bin/python-config --prefix|--exec-prefix|--includes|--libs|--cflags|--ldflags|--extension-suffix|--help|--configdir
strong@foreverstrong:~$

6. python3-config

strong@foreverstrong:~$ python3-config --cflags
-I/usr/include/python3.5m -I/usr/include/python3.5m  -Wno-unused-result -Wsign-compare -g -fstack-protector-strong -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python3-config --ldflags
-L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu -L/usr/lib -lpython3.5m -lpthread -ldl  -lutil -lm  -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python3-config --includes
-I/usr/include/python3.5m -I/usr/include/python3.5m
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python3-config --libs
-lpython3.5m -lpthread -ldl  -lutil -lm 
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python3-config --prefix
/usr
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python3-config --exec-prefix
/usr
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python3-config --extension-suffix
.cpython-35m-x86_64-linux-gnu.so
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python3-config --configdir
/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python3-config --help
Usage: /usr/bin/python3-config --prefix|--exec-prefix|--includes|--libs|--cflags|--ldflags|--extension-suffix|--help|--abiflags|--configdir
strong@foreverstrong:~$

7. EXAMPLES
To build the singe-file c program prog against the python library, use

gcc $(python-config --cflags --ldflags) progr.cpp -o progr.cpp

The same in a makefile:

CFLAGS+=$(shell python-config --cflags)
LDFLAGS+=$(shell python-config --ldflags)
all: progr

To build a dynamically loadable python module, use

gcc $(python-config --cflags --ldflags) -shared -fPIC progr.cpp -o progr.so
strong@foreverstrong:~$ python -c "import numpy; print(numpy.get_include())"
/usr/lib/python2.7/dist-packages/numpy/core/include
strong@foreverstrong:~$
strong@foreverstrong:~$ python -c "import numpy; print(numpy.get_include())"
/home/strong/.local/lib/python2.7/site-packages/numpy/core/include
strong@foreverstrong:~$

8. Makefile - foreverstrong cheng

strong@foreverstrong:~$ python-config --includes
-I/usr/include/python2.7 -I/usr/include/x86_64-linux-gnu/python2.7
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python -c "import numpy; print(numpy.get_include())"
/usr/lib/python2.7/dist-packages/numpy/core/include
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python-config --libs
-lpython2.7 -lpthread -ldl  -lutil -lm 
strong@foreverstrong:~$ 
# Python version
PYTHON_VERSION=2


ifeq ($(PYTHON_VERSION), 2)
PINCLUDE=$(shell python-config --includes)
COMMON+= $(PINCLUDE)

NPINCLUDE=$(shell python -c "import numpy; print(numpy.get_include())")
COMMON+= -I$(NPINCLUDE)

PLIB=$(shell python-config --libs)
LDFLAGS+= $(PLIB)
endif

strong@foreverstrong:~$ python3-config --includes
-I/usr/include/python3.5m -I/usr/include/python3.5m
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python3 -c "import numpy; print(numpy.get_include())"
/usr/local/lib/python3.5/dist-packages/numpy/core/include
strong@foreverstrong:~$ 
strong@foreverstrong:~$ python3-config --libs
-lpython3.5m -lpthread -ldl  -lutil -lm 
strong@foreverstrong:~$
# Python version
PYTHON_VERSION=3


ifeq ($(PYTHON_VERSION), 3)
PINCLUDE=$(shell python3-config --includes)
COMMON+= $(PINCLUDE)

NPINCLUDE=$(shell python3 -c "import numpy; print(numpy.get_include())")
COMMON+= -I$(NPINCLUDE)

PLIB=$(shell python3-config --libs)
LDFLAGS+= $(PLIB)
endif

SEE ALSO
http://docs.python.org/extending/extending.html
/usr/share/doc/python/faq/extending.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值