簡體   English   中英

未知的編譯器標志/參數到cpp

[英]Unknown compiler flag/parameter to cpp

我正在完成pybind11的教程。 要編譯一個例子,我應該使用以下行:

c++ -O3 -shared -std=c++11 -I <path-to-pybind11>/include `python-config --cflags --ldflags` example.cpp -o example.so

我不明白這一部分

`python-config --cflags --ldflags`

它主要不是關於它的內容,而是關於:它在編譯命令中有什么意義? 它屬於-I標志嗎? 那些“`”是什么?

我檢查了c ++ / cpp的手冊,但沒有找到任何東西

反引號

當你在shell命令中看到反引號``之間的東西時,它意味着它是一個單獨的命令,它在主命令之前運行,並且在主命令中使用它寫入標准輸出的任何內容。

例如:

rm `cat file_to_delete.txt`

考慮file_to_delete.txt包含“sausage.png” cat file_to_delete.txt部分首先運行並輸出“sausage.png”然后將其插入主命令,如下所示:

rm sausage.png

你的榜樣是做什么的

所以在你的例子中, python-config --cflags --ldflags是一個獨立於c++命令,無論它輸出什么都在原始命令中被替換。 如果它輸出-Wall -Wextra -lmath你的c++命令最終會像這樣:

c++ -O3 -shared -std=c++11 -I <path-to-pybind11>/include -Wall -Wextra -lmath example.cpp -o example.so

結論

因此, python-config命令的要點是提供標志gccc++實際上使用gcc )將需要用你的python代碼運行你的C ++代碼。

什么

 `python-config --cflags --ldflags`

是執行命令“python-config --cflags --ldflags”並替換輸出(即編譯命令的額外參數)。

程序python-config為您的代碼提供了必要的構建選項。 python-config文檔:

python-config - 用於python C / C ++擴展或嵌入的輸出構建選項

--cflags

打印C編譯器標志。

--ldflags

打印應傳遞給鏈接器的標志。

提供這樣的工具是一種常見的方法,以便自動找到特定系統上的必要構建選項,否則需要用戶自己弄清楚。

在我的Ubuntu 16.04系統上, python-config --cflags --ldflag產生:

-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 -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

所以,這相當於自己這樣做:

c ++ -O3 -shared -std = c ++ 11 -I / include -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 -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 example.cpp -o example.so

現在您可以看到,為什么輔助程序很方便(它可以確定需要哪些庫以及它們位於何處等)。


在相關的說明中,我更喜歡$(python-config --cflags --ldflags)而不是`python-config --cflags --ldflags`,因為推薦$(..)而不是POSIX的反引號。 您可以在“命令替換”部分中查看原理

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM