简体   繁体   English

Python C API 架构 x86_64 的未定义符号

[英]Python C API Undefined symbols for architecture x86_64

I'm trying to compile a file that makes use of Python's C API.我正在尝试编译一个使用 Python 的 C API 的文件。 I'm working in a conda enviroment, running on macOS Monterey.我在 conda 环境中工作,在 macOS Monterey 上运行。 I'm compiling using GCC as following:我正在使用 GCC 进行编译,如下所示:

gcc -c -fPIC file.c -I "${CONDA_PREFIX}/include/python3.9/" -o file.o
gcc file.o -o a.out

The problem is not of inclusion of the <Python.h> header, that is done properly, it seems like once the Python library is included its function's symbols are undefined:问题不在于包含<Python.h> header,如果正确完成,似乎一旦包含 Python 库,它的函数符号是未定义的:

Undefined symbols for architecture x86_64:
  "_PyCallable_Check", referenced from:
      _main in file.o
  "_PyImport_ImportModule", referenced from:
      _main in file.o
  "_PyObject_CallObject", referenced from:
      _main in file.o
  "_PyObject_GetAttrString", referenced from:
      _main in file.o
...

I'm sure the problem isn't with the code since I'm running some demo code from Python's official website.我确定问题不在于代码,因为我正在运行 Python 官方网站上的一些演示代码。 The same error occours when trying to develop a python module using Python C API, compiling as shared library.尝试使用 Python C ZDB974238714CA8DE634A 编译为 1 shared083 库来开发 python 模块时会发生相同的错误。

I also tried compiling the whole thing using distutils/setuptools, the error is kilometric, but the substance is the same as the above one.我还尝试使用 distutils/setuptools 编译整个东西,错误是千米,但实质与上述相同。

The above exception was obtained using this demo code .使用此演示代码获得了上述异常。

Output of ls ${CONDA_PREFIX}/lib ${CONDA_PREFIX}/lib/python3.9 Output 的ls ${CONDA_PREFIX}/lib ${CONDA_PREFIX}/lib/python3.9

/opt/anaconda3/envs/generic/lib:
Tk.icns                 libncurses.6.dylib
Tk.tiff                 libncurses.dylib
clang                   libncursesw.6.dylib
dtrace                  libncursesw.dylib
engines-3               libnode.102.dylib
icu                 libomp.dylib
itcl4.2.1               libpanel.6.dylib
libbz2.1.0.8.dylib          libpanel.dylib
libbz2.a                libpanelw.6.dylib
libbz2.dylib                libpanelw.dylib
libc++.1.0.dylib            libpython3.9.a
libc++.1.dylib              libpython3.9.dylib
libc++.a                libpython3.9.nolto.a
libc++.dylib                libquadmath.0.dylib
libc++experimental.a            libquadmath.dylib
libcrypto.3.dylib           libreadline.8.1.dylib
libcrypto.dylib             libreadline.8.dylib
libffi.8.dylib              libreadline.dylib
libffi.a                librustc-stable_rt.asan.dylib
libffi.dylib                librustc-stable_rt.lsan.dylib
libform.6.dylib             librustc-stable_rt.tsan.dylib
libform.dylib               librustc_driver-5d5e1e2505841b99.dylib
libformw.6.dylib            libsqlite3.0.dylib
libformw.dylib              libsqlite3.dylib
libgcc_s.1.dylib            libssl.3.dylib
libgcc_s_ppc64.1.dylib          libssl.dylib
libgcc_s_x86_64.1.dylib         libstd-dd8a82589e0cba34.dylib
libgfortran.5.dylib         libtcl8.6.dylib
libgfortran.dylib           libtclstub8.6.a
libgomp.1.dylib             libtest-a18e5b5e2a65c941.dylib
libgomp.dylib               libtinfo.6.dylib
libhistory.8.1.dylib            libtinfo.dylib
libhistory.8.dylib          libtinfow.6.dylib
libhistory.dylib            libtinfow.dylib
libicudata.69.1.dylib           libtk8.6.dylib
libicudata.69.dylib         libtkstub8.6.a
libicudata.dylib            libuv.1.dylib
libicui18n.69.1.dylib           libuv.a
libicui18n.69.dylib         libuv.dylib
libicui18n.dylib            libz.1.2.11.dylib
libicuio.69.1.dylib         libz.1.dylib
libicuio.69.dylib           libz.a
libicuio.dylib              libz.dylib
libicutest.69.1.dylib           node_modules
libicutest.69.dylib         ossl-modules
libicutest.dylib            pkgconfig
libicutu.69.1.dylib         python3.9
libicutu.69.dylib           rustlib
libicutu.dylib              sqlite3.34.0
libicuuc.69.1.dylib         tcl8
libicuuc.69.dylib           tcl8.6
libicuuc.dylib              tclConfig.sh
libiomp5.dylib              tclooConfig.sh
liblzma.5.dylib             tdbc1.1.2
liblzma.dylib               tdbcmysql1.1.2
libmenu.6.dylib             tdbcodbc1.1.2
libmenu.dylib               tdbcpostgres1.1.2
libmenuw.6.dylib            terminfo
libmenuw.dylib              thread2.8.6
libncurses++.a              tk8.6
libncurses++w.a             tkConfig.sh

/opt/anaconda3/envs/generic/lib/python3.9:
LICENSE.txt                 mailbox.py
__future__.py                   mailcap.py
__phello__.foo.py               mimetypes.py
__pycache__                 modulefinder.py
_aix_support.py                 multiprocessing
_bootlocale.py                  netrc.py
_bootsubprocess.py              nntplib.py
_collections_abc.py             ntpath.py
_compat_pickle.py               nturl2path.py
_compression.py                 numbers.py
_markupbase.py                  opcode.py
_osx_support.py                 operator.py
_py_abc.py                  optparse.py
_pydecimal.py                   os.py
_pyio.py                    pathlib.py
_sitebuiltins.py                pdb.py
_strptime.py                    pickle.py
_sysconfigdata__darwin_darwin.py        pickletools.py
_sysconfigdata__darwin_darwin.py.orig       pipes.py
_sysconfigdata_x86_64_apple_darwin13_4_0.py pkgutil.py
_threading_local.py             platform.py
_weakrefset.py                  plistlib.py
abc.py                      poplib.py
aifc.py                     posixpath.py
antigravity.py                  pprint.py
argparse.py                 profile.py
ast.py                      pstats.py
asynchat.py                 pty.py
asyncio                     py_compile.py
asyncore.py                 pyclbr.py
base64.py                   pydoc.py
bdb.py                      pydoc_data
binhex.py                   queue.py
bisect.py                   quopri.py
bz2.py                      random.py
cProfile.py                 re.py
calendar.py                 reprlib.py
cgi.py                      rlcompleter.py
cgitb.py                    runpy.py
chunk.py                    sched.py
cmd.py                      secrets.py
code.py                     selectors.py
codecs.py                   shelve.py
codeop.py                   shlex.py
collections                 shutil.py
colorsys.py                 signal.py
compileall.py                   site-packages
concurrent                  site.py
config-3.9-darwin               smtpd.py
configparser.py                 smtplib.py
contextlib.py                   sndhdr.py
contextvars.py                  socket.py
copy.py                     socketserver.py
copyreg.py                  sqlite3
crypt.py                    sre_compile.py
csv.py                      sre_constants.py
ctypes                      sre_parse.py
curses                      ssl.py
dataclasses.py                  stat.py
datetime.py                 statistics.py
dbm                     string.py
decimal.py                  stringprep.py
difflib.py                  struct.py
dis.py                      subprocess.py
distutils                   sunau.py
doctest.py                  symbol.py
email                       symtable.py
encodings                   sysconfig.py
ensurepip                   tabnanny.py
enum.py                     tarfile.py
filecmp.py                  telnetlib.py
fileinput.py                    tempfile.py
fnmatch.py                  test
formatter.py                    textwrap.py
fractions.py                    this.py
ftplib.py                   threading.py
functools.py                    timeit.py
genericpath.py                  tkinter
getopt.py                   token.py
getpass.py                  tokenize.py
gettext.py                  trace.py
glob.py                     traceback.py
graphlib.py                 tracemalloc.py
gzip.py                     tty.py
hashlib.py                  turtle.py
heapq.py                    turtledemo
hmac.py                     types.py
html                        typing.py
http                        unittest
idlelib                     urllib
imaplib.py                  uu.py
imghdr.py                   uuid.py
imp.py                      venv
importlib                   warnings.py
inspect.py                  wave.py
io.py                       weakref.py
ipaddress.py                    webbrowser.py
json                        wsgiref
keyword.py                  xdrlib.py
lib-dynload                 xml
lib2to3                     xmlrpc
linecache.py                    zipapp.py
locale.py                   zipfile.py
logging                     zipimport.py
lzma.py                     zoneinfo

My CONDA_PREFIX is /opt/anaconda3/envs/generic我的CONDA_PREFIX/opt/anaconda3/envs/generic

This command: gcc file.o -o a.out does not link to a python library.此命令: gcc file.o -o a.out链接到 python 库。

You need to add -lpython3 and possibly -L${CONDA_PREFIX}/lib/python3.9 to it.您需要添加-lpython3和可能的-L${CONDA_PREFIX}/lib/python3.9

Try adding this to your linking command:尝试将此添加到您的链接命令中:

-L${CONDA_PREFIX}/lib -lpython3.9

This should tell the linker to use the libpython3.9.a file I see in your directory listing.这应该告诉 linker 使用我在您的目录列表中看到的 libpython3.9.a 文件。

By the way, I imagine Conda comes with some configuration tool that outputs these same options, so you wouldn't have to hardcore the Python version number.顺便说一句,我想 Conda 带有一些输出这些相同选项的配置工具,因此您不必硬核 Python 版本号。

暂无
暂无

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

相关问题 osx-体系结构x86_64的boost和python未定义符号 - osx - boost and python undefined symbols for architecture x86_64 PyQt安装10.7“架构x86_64的未定义符号” - PyQt installation 10.7 “Undefined symbols for architecture x86_64” 架构 x86_64 的未定义符号:“_PyUnicode_Type”,引用自:when using python_boost - Undefined symbols for architecture x86_64: “_PyUnicode_Type”, referenced from: when using python_boost OpenPose:架构 x86_64 的未定义符号:libcaffeproto.a(caffe.pb.cc.o) - OpenPose : Undefined symbols for architecture x86_64: libcaffeproto.a(caffe.pb.cc.o) Python扩展:找不到架构x86_64错误的符号 - Python extension: symbol(s) not found for architecture x86_64 error arch x86_64 和 arm64e 可用但 python3 在 Mac M1 上说不兼容的架构 - arch x86_64 and arm64e is available but python3 is saying incompatible architecture on Mac M1 Kivy Mac 为 x86 架构抛出未定义的符号 - Kivy Mac throwing undefined symbols for x86 architecture (mach-o 文件,但它是一个不兼容的架构(有 'arm64',需要 'x86_64')) - (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')) 如何创建可以在arm64和x86_64架构之间切换的conda环境? - How to create a conda environment that can switch between arm64 and x86_64 architecture? (mach-o 文件,但架构不兼容(有'x86_64',需要'arm64e')) - (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM