简体   繁体   中英

Unknown compiler flag/parameter to cpp

I'm working through the tutorial of 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? What's with those "`" ?

I checked the manual of c++/cpp, but didn't find anything

The backquotes

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.

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:

rm sausage.png

What your example does

So in your example, python-config --cflags --ldflags is a separate command from c++ and whatever it outputs is replaced in the original command. If it outputs -Wall -Wextra -lmath your c++ command will end up like this:

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

Conclusion

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.

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

The program python-config provides the necessary build options for your code. From python-config documentation:

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

--cflags

print the C compiler flags.

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

-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

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. You can see the rationale here under section "Command Substitution".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM