Search code examples
c++gcccompiler-flags

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


Solution

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