首页 > 解决方案 > 在 Mac Os 中编译和链接 Python 模块

问题描述

我正在开发一个 Python 模块。我有 C 源文件和一个编译库。在 Mac Os 中链接时遇到问题,所以我按照Python runtime_library_dirs doesn't work on Mac提供的说明进行操作。

这篇文章说在 Mac Os 中链接时应该添加额外的链接参数。它还说应该使用 install_name_tool 来更改库的安装名称。

但是,我在使用 install_name_tool 时收到此错误消息:

string table not at the end of the file (can't be processed) in file:

该库是从 Go 源代码编译的。

标签: pythoncgo

解决方案


好的。这是我以前的:

ext_modules = [Extension("_mypymod", ["mymod.c"],
                         extra_link_args=[],
                         depends=[],
                         libraries = [':mylib.a'],
                         library_dirs = [
                             lib_path
                         ],
                   )],

这在 MacOS 中不起作用。应该是这样的:

ext_modules = [Extension("_mypymod", ["mymod.c"],
                             extra_link_args=[lib_path + '/mylib.a'], #full path to the library
                             depends=[],
                       )],

这适用于linux和Mac。


推荐阅读