Python flags: --cflags, --includes, --ldflags, --libs
如果將cpp檔案編譯成一個so檔,可以使用如下指令:
gcc $(python-config --cflags --ldflags) -shared -fPIC progr.cpp -o progr.so
其中的--cflags
和--ldflags
是什麼呢?
參考python3-config (1) - Linux Manuals:
--cflags
print the C compiler flags.
--ldflags
print the flags that should be passed to the linker.
--includes
similar to --cflags but only with -I options (path to python header files).
--libs
similar to --ldflags but only with -l options (used libraries).
--includes
是--cflags
的子集,只包含-I
的部份。
--libs
是--ldflags
的子集,只包含-l
的部份。
驗證如下:
python3-config --incldues
展開來為:
-I/usr/include/python3.8 -I/usr/include/python3.8
python3-config --cflags
展開來為:
-I/usr/include/python3.8 -I/usr/include/python3.8 -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.8-uvizni/python3.8-3.8.10=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall
包含了python3-config --incldues
。
python3-config --libs
展開來為:
-lcrypt -lpthread -ldl -lutil -lm -lm
python3-config --ldflags
展開來為:
-L/usr/lib/python3.8/config-3.8-x86_64-linux-gnu -L/usr/lib -lcrypt -lpthread -ldl -lutil -lm -lm
包含了python3-config --libs
。