简体   繁体   English

将Python C扩展链接到另一个库的问题

[英]Issue with Linking Python C Extension to Another Library

I am in the process of developing a Python extension to allow a script running on a Raspberry Pi to control a sensor . 我正在开发Python扩展,以允许在Raspberry Pi上运行的脚本控制传感器 The sensor manufacturer provided my organization with the source code to their C API, and I am trying to create a set of bindings to make the sensor accessible in Python scrips. 传感器制造商向我的组织提供了其C API的源代码,并且我正尝试创建一组绑定以使传感器可以在Python脚本中访问。

The makefile that came with the API source created a set of object files that I then linked together into a library (libvl53l1.a) using the command: API源附带的makefile创建了一组目标文件,然后使用以下命令将它们链接到一个库(libvl53l1.a)中:

ar -cvq libvl53l1.a *.o ar -cvq libvl53l1.a * .o

I then added this library to the setup.py script o my extension by adding this flag: 然后,我通过添加以下标志将此库添加到我的扩展程序的setup.py脚本中:

extra_compile_args=["-l:libvl53l1.a"] extra_compile_args = [ “ - 1:libvl53l1.a”]

The code, library, and setup.py script are currently in the same directory for convenience. 为了方便起见,代码,库和setup.py脚本当前位于同一目录中。 Installing the library into Python using the command (python3 setup.py build_ext --inplace) runs without errors, however, when I try to import my library in a Python interpreter, the import fails because of an undefined symbol "VL53L1_WaitDeviceBooted" in the extension's .so file. 使用命令(python3 setup.py build_ext --inplace)将库安装到Python时运行没有错误,但是,当我尝试在Python解释器中导入库时,由于扩展名中未定义的符号“ VL53L1_WaitDeviceBooted”,导入失败.so文件。 Listing the symbols in libvl54l1.a: 列出libvl54l1.a中的符号:

nm libvl53l1.a | nm libvl53l1.a | grep "VL53L1_WaitDeviceBooted" grep“ VL53L1_WaitDeviceBooted”

shows that the library does expose a symbol of this name. 显示该库确实公开了该名称的符号。 Therefore, I believe that the linker is failing to link the extension with this static library. 因此,我认为链接器无法将扩展名与此静态库链接。 Is there a step I am missing that is causing this? 我是否缺少导致此问题的步骤? I have also tried removing the .a extension as recommended in the Python documentation, to no avail. 我也曾尝试删除Python文档中建议的.a扩展名,但无济于事。

Thank you 谢谢

extra_compile_args=["-l:libvl53l1.a"]

This setting adds -l:... to the compilation command, but the compiler ignores that option, because it's a linking option, and no linking is done by the compiler. 此设置将-l:...添加到编译命令,但是编译器将忽略该选项,因为它是一个链接选项,并且编译器不执行任何链接。

You want: extra_link_args=["-lvl53l1"] , which would add -lvl53l1 to the link command (the linker wouldn't ignore that option while performing the linking). 您需要: extra_link_args=["-lvl53l1"] ,这会将-lvl53l1添加到链接命令中(链接器在执行链接时不会忽略该选项)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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