[英]Unknown compiler flag/parameter to cpp
I'm working through the tutorial of pybind11 . 我正在完成pybind11的教程。 To compile an example, I'm supposed to use the following line:
要编译一个例子,我应该使用以下行:
c++ -O3 -shared -std=c++11 -I <path-to-pybind11>/include `python-config --cflags --ldflags` example.cpp -o example.so
I do not understand the part 我不明白这一部分
`python-config --cflags --ldflags`
It's not primarily about its content, it's more about: What meaning does it have in the compile command? 它主要不是关于它的内容,而是关于:它在编译命令中有什么意义? Does it belong to the
-I
flag? 它属于
-I
标志吗? What's with those "`" ? 那些“`”是什么?
I checked the manual of c++/cpp, but didn't find anything 我检查了c ++ / cpp的手册,但没有找到任何东西
When in shell commands you see stuff between backquotes ``, it means that it's a separate command that is run before the main one and whatever it writes to standard output is used in the main command. 当你在shell命令中看到反引号``之间的东西时,它意味着它是一个单独的命令,它在主命令之前运行,并且在主命令中使用它写入标准输出的任何内容。
For example: 例如:
rm `cat file_to_delete.txt`
Consider file_to_delete.txt
contains "sausage.png" The cat file_to_delete.txt
part is run first and outputs "sausage.png" This is then inserted into the main command as follows: 考虑
file_to_delete.txt
包含“sausage.png” cat file_to_delete.txt
部分首先运行并输出“sausage.png”然后将其插入主命令,如下所示:
rm sausage.png
So in your example, python-config --cflags --ldflags
is a separate command from c++
and whatever it outputs is replaced in the original command. 所以在你的例子中,
python-config --cflags --ldflags
是一个独立于c++
命令,无论它输出什么都在原始命令中被替换。 If it outputs -Wall -Wextra -lmath
your c++
command will end up like this: 如果它输出
-Wall -Wextra -lmath
你的c++
命令最终会像这样:
c++ -O3 -shared -std=c++11 -I <path-to-pybind11>/include -Wall -Wextra -lmath example.cpp -o example.so
The point of the python-config
command is thus to provide the flags gcc
( c++
actually uses gcc
) will need to run your C++ code with your python code. 因此,
python-config
命令的要点是提供标志gcc
( c++
实际上使用gcc
)将需要用你的python代码运行你的C ++代码。
What 什么
`python-config --cflags --ldflags`
does is execute the command "python-config --cflags --ldflags" and replace the output (ie extra arguments to your compilation command). 是执行命令“python-config --cflags --ldflags”并替换输出(即编译命令的额外参数)。
The program python-config
provides the necessary build options for your code. 程序
python-config
为您的代码提供了必要的构建选项。 From python-config
documentation: 从
python-config
文档:
python-config - output build options for python C/C++ extensions or embedding
python-config - 用于python C / C ++扩展或嵌入的输出构建选项
--cflags
--cflags
print the C compiler flags.
打印C编译器标志。
--ldflags
--ldflags
print the flags that should be passed to the linker.
打印应传递给链接器的标志。
Providing such a tool is a common approach so that the necessary build options on a particular is system is automatically found, which would otherwise require the users to figure out themselves. 提供这样的工具是一种常见的方法,以便自动找到特定系统上的必要构建选项,否则需要用户自己弄清楚。
On my Ubuntu 16.04 system, python-config --cflags --ldflag
produces: 在我的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
-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
So, it's equivalent to doing this myself: 所以,这相当于自己这样做:
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
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
Now you can see, why the helper program is handy (it figures out what libraries are needed and where they are located etc). 现在您可以看到,为什么辅助程序很方便(它可以确定需要哪些库以及它们位于何处等)。
On a relevant note, I prefer $(python-config --cflags --ldflags)
instead of `python-config --cflags --ldflags` as the $(..)
is recommended over backticks by POSIX. 在相关的说明中,我更喜欢
$(python-config --cflags --ldflags)
而不是`python-config --cflags --ldflags`,因为推荐$(..)
而不是POSIX的反引号。 You can see the rationale here under section "Command Substitution". 您可以在“命令替换”部分中查看原理 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.