gcc/ld:使用-Wl,-rpath -Wl调用TensorflowLite库

问题描述 投票:0回答:0

我正在使用 Pybind11 运行在 Python 中使用 TensorflowLite 的 C++ 库。目前我的项目结构如下:

项目结构:

*include: (folder with header files)
 ---- model.hpp
*lib: (folder with the libraries to run in python)
 ---- libmy_model.so
 ---- libtensorflowlite.so
*model: (folder with the current tflite model)
 ---- tfmodel.tflite
*tasks.py (Python file to add 'invoke' commands)
  • libmymodel.so 依赖于 libtensorflowlite.so 运行

所以使用“调用”我有一个函数来构建 pybind11 库:

invoke.run(
        "g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC "
        "`python3 -m pybind11 --includes` "
        "-I . "
        "{0} "
        "-o {1}`python3-config --extension-suffix` "
        "-Llib -lmymodel -ltensorflowlite -Wl,-rpath,lib".format(cpp_name, extension_name))

但是,当我运行构建Pybind11库的命令时,报错:

ImportError: libtensorflowlite.so: cannot open shared object file: No such file or directory

根据我的理解,要添加多个库,我可以使用 -L(dir) 引用所有库所在的目录,并使用 -Wl,-rpath 链接它们。但它似乎无法识别 libtensorflowlite.so 库。

尽管如此,如果我添加所有库outside lib文件夹并将它们添加到源路径中,如下所示:

项目结构:

*include: (folder with header files)
 ---- model.hpp
*lib: (folder with the libraries empty)
*model: (folder with the current tflite model)
 ---- tfmodel.tflite
*tasks.py (Python file to add 'invoke' commands)
*libmy_model.so
*libtensorflowlite.so

构建 pybind 代码如下:

invoke.run(
        "g++ -O3 -Wall -Werror -shared -std=c++11 -fPIC "
        "`python3 -m pybind11 --includes` "
        "-I . "
        "{0} "
        "-o {1}`python3-config --extension-suffix` "
        "-L. -lmymodel -Wl,-rpath,.".format(cpp_name, extension_name))

完美无缺。

但是,由于模型很多,我需要将所有库都放在 lib 文件夹中。我认为错误是在我添加带有 -L,-Wl,-rpath 的库的行中,这些库没有正确链接到 lib 文件夹。

我将不胜感激任何反馈。比你。

c++11 gcc ld tensorflow-lite pybind11
© www.soinside.com 2019 - 2024. All rights reserved.