![](/img/trans.png)
[英]Python / C++ binding, how to link agains static c++ library (portaudio) with distutils?
[英]how to link python static library with my c++ program
我正在實現一個使用python / C ++ Extensions的C ++程序。 截至目前,我明確地將我的程序鏈接到我編譯的python靜態庫。 我想知道有沒有辦法將我的程序與系統安裝的python鏈接(我的意思是linux附帶的默認python安裝)
是。 有一個名為python-config
的命令行實用程序:
Usage: /usr/bin/python-config [--prefix|--exec-prefix|--includes|--libs|--cflags|--ldflags|--help]
出於鏈接目的,您必須使用--ldflags
參數調用它。 它將打印您必須傳遞給鏈接器(或g++
)的標志列表,以便與系統安裝的python庫鏈接:
$ python-config --ldflags
-L/usr/lib/python2.6/config -lpthread -ldl -lutil -lm -lpython2.6
它還可以為您提供使用--cflags
參數進行編譯的標志:
$ python-config --cflags
-I/usr/include/python2.6 -I/usr/include/python2.6 -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes
假設你在test.cpp
文件中有一個測試程序,那么你可以做這樣的事情來編譯和鏈接:
g++ $(python-config --cflags) -o test $(python-config --ldflags) ./test.cpp
這會將您的程序與共享庫鏈接起來。 如果要轉為靜態,可以將-static
選項傳遞給鏈接器。 但這將與所有靜態內容相關聯,包括運行時。 如果你想只使用靜態python,你必須自己找到這些庫。 其中一個選項是解析python-config --ldflags
輸出並查找帶有.a
擴展名的庫。 但我寧願堅持所有動態或全靜態。
希望能幫助到你。 祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.