'Using makefile with pybind11

I am trying to manually compile a c++ project that uses pybind11. I am able to make it work but I don't understand one compilation problem.

Here is a minimal example: I have a folder containing the makefile and folders lib/, bin/ and src/. src/ contains the following files implementing a function that is just returns 1.

src/hi.hpp

#ifndef hi_hpp
#define hi_hpp

#include <stdio.h>

int get1();

#endif

src/hi.cpp

#include "hi.hpp"

int get1() {
    return 1;
}

src/bind_hi.cpp

#include "hi.hpp"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>

namespace py = pybind11;

PYBIND11_MODULE( hi, m ) {
    m.doc() = "doc"; // optional module docstring
    
    m.def("get1", &get1, "get 1");
}

makefile (working)

CXX:=clang++
CXXFLAGS:=-Ofast -Wall -shared -std=c++11 -undefined dynamic_lookup -fPIC -I/Users/user/opt/anaconda3/include/python3.8 -I/Users/user/opt/anaconda3/lib/python3.8/site-packages/pybind11/include -I/src

EXT:=$(shell python3-config --extension-suffix)

DEPS:= src/hi.cpp src/bind_hi.cpp

hi.$(EXT): $(DEPS)
    $(CXX) $(CXXFLAGS) $(DEPS) -o lib/hi$(EXT) -v

clean:
    @rm lib/*$(EXT)
    
.PHONY=clean

Here is the output I get during compilation:

clang++ -Ofast -Wall -shared -std=c++11 -undefined dynamic_lookup -fPIC -I/Users/user/opt/anaconda3/include/python3.8 -I/Users/user/opt/anaconda3/lib/python3.8/site-packages/pybind11/include -I/src src/hi.cpp src/bind_hi.cpp -o lib/hi.cpython-38-darwin.so -v
clang version 10.0.0 
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Users/user/opt/anaconda3/bin
 "/Users/user/opt/anaconda3/bin/clang-10" -cc1 -triple x86_64-apple-macosx10.16.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name hi.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -menable-no-infs -menable-no-nans -menable-unsafe-fp-math -fno-signed-zeros -mreassociate -freciprocal-math -ffp-contract=fast -fno-rounding-math -ffast-math -ffinite-math-only -masm-verbose -munwind-tables -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -target-linker-version 241.9 -v -resource-dir /Users/user/opt/anaconda3/lib/clang/10.0.0 -I /Users/user/opt/anaconda3/include/python3.8 -I /Users/user/opt/anaconda3/lib/python3.8/site-packages/pybind11/include -I /src -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -stdlib=libc++ -internal-isystem /Users/user/opt/anaconda3/bin/../include/c++/v1 -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1 -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Users/user/opt/anaconda3/lib/clang/10.0.0/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -Ofast -Wall -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Users/user/Documents/python_package_example -ferror-limit 19 -fmessage-length 158 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fobjc-runtime=macosx-10.16.0 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -o /var/folders/xy/lcpszqt54bg9_4xnrhdk9j500000gp/T/hi-ffd9cb.o -x c++ src/hi.cpp
clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target x86_64-apple-darwin20.3.0
ignoring nonexistent directory "/Users/user/opt/anaconda3/lib/python3.8/site-packages/pybind11/include"
ignoring nonexistent directory "/src"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1"
ignoring nonexistent directory
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:

/Users/user/opt/anaconda3/include/python3.8
/Users/user/opt/anaconda3/bin/../include/c++/v1
/Users/user/opt/anaconda3/lib/clang/10.0.0/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/Users/user/opt/anaconda3/bin/clang-10" -cc1 -triple x86_64-apple-macosx10.16.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name bind_hi.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -menable-no-infs -menable-no-nans -menable-unsafe-fp-math -fno-signed-zeros -mreassociate -freciprocal-math -ffp-contract=fast -fno-rounding-math -ffast-math -ffinite-math-only -masm-verbose -munwind-tables -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -target-linker-version 241.9 -v -resource-dir /Users/user/opt/anaconda3/lib/clang/10.0.0 -I /Users/user/opt/anaconda3/include/python3.8 -I /Users/user/opt/anaconda3/lib/python3.8/site-packages/pybind11/include -I /src -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -stdlib=libc++ -internal-isystem /Users/user/opt/anaconda3/bin/../include/c++/v1 -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1 -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Users/user/opt/anaconda3/lib/clang/10.0.0/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -Ofast -Wall -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Users/user/Documents/python_package_example -ferror-limit 19 -fmessage-length 158 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fobjc-runtime=macosx-10.16.0 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -o /var/folders/xy/lcpszqt54bg9_4xnrhdk9j500000gp/T/bind_hi-4642ed.o -x c++ src/bind_hi.cpp
clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target x86_64-apple-darwin20.3.0
ignoring nonexistent directory "/Users/user/opt/anaconda3/lib/python3.8/site-packages/pybind11/include"
ignoring nonexistent directory "/src"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /Users/user/opt/anaconda3/include/python3.8
 /Users/user/opt/anaconda3/bin/../include/c++/v1
 /Users/user/opt/anaconda3/lib/clang/10.0.0/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
src/bind_hi.cpp:9:10: fatal error: 'pybind11/pybind11.h' file not found
#include <pybind11/pybind11.h>
         ^~~~~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [hi..cpython-38-darwin.so] Error 1

This output deviates from that during successful compilation at the line:

ignoring nonexistent directory "/Users/user/opt/anaconda3/lib/python3.8/site-packages/pybind11/include"


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source