OSX 的 Cython Link 错误
Cython Link Errors for OSX
我有一个与此非常相似的问题;但是,在实施了建议的修复后 - 我仍然收到以下错误。
我正在尝试编译由 Cython 生成的 C 文件。
cython ConnectFour.pyx --embed
我尝试像这样编译我的 c 文件:
gcc -I /usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/Headers -o ConnectFour ConnectFour.c
但是我收到以下错误:
Undefined symbols for architecture x86_64:
"_PyBaseObject_Type", referenced from:
___Pyx_InBases in ConnectFour-3c26d7.o
"_PyBytes_FromStringAndSize", referenced from:
___pyx_pymod_exec_ConnectFour in ConnectFour-3c26d7.o
___Pyx_InitStrings in ConnectFour-3c26d7.o
"_PyCFunction_NewEx", referenced from:
___pyx_pymod_exec_ConnectFour in ConnectFour-3c26d7.o
"_PyCFunction_Type", referenced from:
___Pyx_PyObject_CallOneArg in ConnectFour-3c26d7.o
___Pyx_PyObject_CallNoArg in ConnectFour-3c26d7.o
___Pyx_PyObject_Call2Args in ConnectFour-3c26d7.o
___Pyx_PyCFunction_FastCall in ConnectFour-3c26d7.o
___pyx_pf_11ConnectFour_36eval_heuristic_score in ConnectFour-3c26d7.o
___pyx_pf_11ConnectFour_38list_valid_col_idxs in ConnectFour-3c26d7.o
___pyx_pf_11ConnectFour_40deep_copy_board in ConnectFour-3c26d7.o
...
"_PyCode_New", referenced from:
___Pyx_InitCachedConstants in ConnectFour-3c26d7.o
___Pyx_CreateCodeObjectForTraceback in ConnectFour-3c26d7.o
"_PyDict_Copy", referenced from:
___pyx_pw_11ConnectFour_7profile_1wrapper in ConnectFour-3c26d7.o
___pyx_pw_11ConnectFour_5timer_1wrapper in ConnectFour-3c26d7.o
"_PyDict_GetItemString", referenced from:
___pyx_pymod_exec_ConnectFour in ConnectFour-3c26d7.o
"_PyDict_GetItemWithError", referenced from:
___Pyx_PyDict_GetItem in ConnectFour-3c26d7.o
"_PyDict_New", referenced from:
___pyx_pymod_exec_ConnectFour in ConnectFour-3c26d7.o
___Pyx_Import in ConnectFour-3c26d7.o
___Pyx_CyFunction_get_dict in ConnectFour-3c26d7.o
___Pyx_CyFunction_get_annotations in ConnectFour-3c26d7.o
___pyx_pf_11ConnectFour_28count_players in ConnectFour-3c26d7.o
___pyx_pf_11ConnectFour_52num_in_a_row in ConnectFour-3c26d7.o
我的出现是基于 and this 所以 post 我缺少一些图书馆链接?所以我尝试了以下方法。
gcc -I /usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/Headers -o ConnectFour ConnectFour.c -l/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7.dylib
现在我收到一个找不到库的错误。
ld: library not found for
-l/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7.dylib
我完全无法阅读所有这些 SO posts(第一次与 C 打交道)所以会逐步回答。
我需要什么才能编译然后 运行 我的 C 文件没有 error-ing 出来?
我知道我的 headers 位于 /usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/Headers
而图书馆位于 /usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7.dylib
我在 OSX 并且我使用 Homebrew 安装了 Python。
尝试运行
gcc -I /usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/Headers -o ConnectFour ConnectFour.c -L/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7 -lpython3.7
-L 标志将该目录添加到搜索路径,然后使用 -l 到 link(因为 -l 似乎不接受绝对路径),在 Linux 至少 lib使用 -l 时省略了前缀和扩展名,但我从未使用过 MacOSX,所以如果不起作用,请尝试不同的组合。
所以我最终根据@ead 的建议更仔细地阅读了 ,事实证明这是一个很大的帮助。写下给遇到类似问题的人。
在 运行ning cython ConnectFour.pyx --embed
之后,我的目标是将 .c 文件编译成可执行文件。这意味着 (1) 需要构建 .o 目标文件和 (2) 将 .o 目标文件转换为 运行nable 的可执行文件,如下所示:./MyExecutable
步骤 1. 构建 .o 目标文件
我们需要运行以下但我遇到的问题是找出如何获得我的标志。
gcc -c hello.c -o hello.o <our cflags>
由于我通过 Homebrew 安装了 Python,因此 Python 及其头文件位于以下目录中。
/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/bin/
为了为我生成 cflags - 我必须 运行 以下命令。请注意 --cflags 标志。命令 运行 是我的 Python 3 安装的配置实用程序,它安装在奇怪的目录中(因为我通过 Homebrew 安装了 Python)。
/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/bin/python3.7-config
--cflags
下面是它为我生成的标志的片段。
-I/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/include/python3.7m -I/usr/local.......etc etc.
使用我 运行 以下的标志来生成我的目标文件。
gcc -c ConnectFour.c -o ConnectFour.o -I/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/include/python3.7m -I/...etc etc.
步骤 2. 将目标文件转换为可执行文件
为了生成可执行文件,我们需要运行以下内容。
gcc main.o hello.o -o prog <our ldflags>
为了生成 ldflags,这次我需要 运行 以下命令。我们再次使用配置实用程序,但这次使用 --ldflags.
/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/bin/python3.7-config --ldflags
这为我生成了以下标志。
-L/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/config-3.7m-darwin -lpython3.7m ...etc. etc.
使用这些标志,我运行生成我的可执行文件。
gcc ConnectFour.o -o ConnectFour -L/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/config-3.7m-darwin -lpython3.7m ...etc. etc.
完成!弹出一个可执行文件,我 运行 它就像任何其他 C 可执行文件一样。
特别感谢@ead 为我指明了正确的方向。
我有一个与此非常相似的问题
我正在尝试编译由 Cython 生成的 C 文件。
cython ConnectFour.pyx --embed
我尝试像这样编译我的 c 文件:
gcc -I /usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/Headers -o ConnectFour ConnectFour.c
但是我收到以下错误:
Undefined symbols for architecture x86_64:
"_PyBaseObject_Type", referenced from:
___Pyx_InBases in ConnectFour-3c26d7.o
"_PyBytes_FromStringAndSize", referenced from:
___pyx_pymod_exec_ConnectFour in ConnectFour-3c26d7.o
___Pyx_InitStrings in ConnectFour-3c26d7.o
"_PyCFunction_NewEx", referenced from:
___pyx_pymod_exec_ConnectFour in ConnectFour-3c26d7.o
"_PyCFunction_Type", referenced from:
___Pyx_PyObject_CallOneArg in ConnectFour-3c26d7.o
___Pyx_PyObject_CallNoArg in ConnectFour-3c26d7.o
___Pyx_PyObject_Call2Args in ConnectFour-3c26d7.o
___Pyx_PyCFunction_FastCall in ConnectFour-3c26d7.o
___pyx_pf_11ConnectFour_36eval_heuristic_score in ConnectFour-3c26d7.o
___pyx_pf_11ConnectFour_38list_valid_col_idxs in ConnectFour-3c26d7.o
___pyx_pf_11ConnectFour_40deep_copy_board in ConnectFour-3c26d7.o
...
"_PyCode_New", referenced from:
___Pyx_InitCachedConstants in ConnectFour-3c26d7.o
___Pyx_CreateCodeObjectForTraceback in ConnectFour-3c26d7.o
"_PyDict_Copy", referenced from:
___pyx_pw_11ConnectFour_7profile_1wrapper in ConnectFour-3c26d7.o
___pyx_pw_11ConnectFour_5timer_1wrapper in ConnectFour-3c26d7.o
"_PyDict_GetItemString", referenced from:
___pyx_pymod_exec_ConnectFour in ConnectFour-3c26d7.o
"_PyDict_GetItemWithError", referenced from:
___Pyx_PyDict_GetItem in ConnectFour-3c26d7.o
"_PyDict_New", referenced from:
___pyx_pymod_exec_ConnectFour in ConnectFour-3c26d7.o
___Pyx_Import in ConnectFour-3c26d7.o
___Pyx_CyFunction_get_dict in ConnectFour-3c26d7.o
___Pyx_CyFunction_get_annotations in ConnectFour-3c26d7.o
___pyx_pf_11ConnectFour_28count_players in ConnectFour-3c26d7.o
___pyx_pf_11ConnectFour_52num_in_a_row in ConnectFour-3c26d7.o
我的出现是基于
gcc -I /usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/Headers -o ConnectFour ConnectFour.c -l/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7.dylib
现在我收到一个找不到库的错误。
ld: library not found for -l/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7.dylib
我完全无法阅读所有这些 SO posts(第一次与 C 打交道)所以会逐步回答。
我需要什么才能编译然后 运行 我的 C 文件没有 error-ing 出来?
我知道我的 headers 位于 /usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/Headers
而图书馆位于 /usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7.dylib
我在 OSX 并且我使用 Homebrew 安装了 Python。
尝试运行
gcc -I /usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/Headers -o ConnectFour ConnectFour.c -L/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7 -lpython3.7
-L 标志将该目录添加到搜索路径,然后使用 -l 到 link(因为 -l 似乎不接受绝对路径),在 Linux 至少 lib使用 -l 时省略了前缀和扩展名,但我从未使用过 MacOSX,所以如果不起作用,请尝试不同的组合。
所以我最终根据@ead 的建议更仔细地阅读了
在 运行ning cython ConnectFour.pyx --embed
之后,我的目标是将 .c 文件编译成可执行文件。这意味着 (1) 需要构建 .o 目标文件和 (2) 将 .o 目标文件转换为 运行nable 的可执行文件,如下所示:./MyExecutable
步骤 1. 构建 .o 目标文件
我们需要运行以下但我遇到的问题是找出如何获得我的标志。
gcc -c hello.c -o hello.o <our cflags>
由于我通过 Homebrew 安装了 Python,因此 Python 及其头文件位于以下目录中。
/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/bin/
为了为我生成 cflags - 我必须 运行 以下命令。请注意 --cflags 标志。命令 运行 是我的 Python 3 安装的配置实用程序,它安装在奇怪的目录中(因为我通过 Homebrew 安装了 Python)。
/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/bin/python3.7-config
--cflags
下面是它为我生成的标志的片段。
-I/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/include/python3.7m -I/usr/local.......etc etc.
使用我 运行 以下的标志来生成我的目标文件。
gcc -c ConnectFour.c -o ConnectFour.o -I/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/include/python3.7m -I/...etc etc.
步骤 2. 将目标文件转换为可执行文件
为了生成可执行文件,我们需要运行以下内容。
gcc main.o hello.o -o prog <our ldflags>
为了生成 ldflags,这次我需要 运行 以下命令。我们再次使用配置实用程序,但这次使用 --ldflags.
/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/bin/python3.7-config --ldflags
这为我生成了以下标志。
-L/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/config-3.7m-darwin -lpython3.7m ...etc. etc.
使用这些标志,我运行生成我的可执行文件。
gcc ConnectFour.o -o ConnectFour -L/usr/local/opt/python/Frameworks/Python.framework/Versions/3.7/lib/python3.7/config-3.7m-darwin -lpython3.7m ...etc. etc.
完成!弹出一个可执行文件,我 运行 它就像任何其他 C 可执行文件一样。
特别感谢@ead 为我指明了正确的方向。