I want to compile a C++ file which uses Python embedding. Therefore I #include in my C++ source. When using g++ as compiler I would specify the following flags:
g++ -o pybridge pybridge.cc -I/usr/include/python2.7/ -lpython2.7
I want to use Bazel for compilation now and tried the following target rule:
cc_binary(
name = "pybridge",
srcs = ["pybridge.cc"],
copts = ["-I/usr/include/python2.7/"],
linkopts = ["-lpython2.7"]
)
Running bazel build
gives error messages like this:
pybridge.cc:(.text+0x10): undefined reference to Py_Initialize
Bazel executes your build in a sandbox, to prevent it from accessing random resources on your system that won't be present on, say, your coworker's system.
This means that, unless you've declared a file (like the Python library) as a dependency, Bazel won't put it in the sandbox and your build won't be able to find it.
There are two options:
The easy one is to build with --spawn_strategy=standalone
( bazel build --spawn_strategy=standalone :pybridge
). This tells Bazel not to use sandboxing for this build. Note that, as far as Bazel knows, nothing has changed between the sandboxed and non-sandboxed run, so you'll have to clean before re-running without sandboxing, or you'll just get the cached error.
The harder option is to declare /usr/lib/libpython2.7.so
as an input to your build. If you want to do that, add the following to the WORKSPACE file:
local_repository(
name = "system_python",
path = "/usr/lib/python2.7/config-x86_64-linux-gnu", # Figure out where it is on your system, this is where it is on mine
build_file_content = """
cc_library(
name = "my-python-lib",
srcs = ["libpython2.7.so"],
visibility = ["//visibility:public"],
)
""",
)
Then in your BUILD file, instead of linkopts = ["-lpython2.7"]
, add deps = ["@system_python//:my-python-lib"]
. Then Bazel will understand that your build depends on libpython2.7.so (and include it in the sandbox).
( Tried commenting on OP's post but I lack the required karma. )
FWIW, I've had problems linking against Python 2.7's libraries (on Windows), even when I didn't use Bazel but ran the linker manually, so this problem may be unrelated to Bazel.
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.