Home > database >  Python C API Undefined symbols for architecture x86_64
Python C API Undefined symbols for architecture x86_64

Time:01-02

I'm trying to compile a file that makes use of Python's C API. I'm working in a conda enviroment, running on macOS Monterey. I'm compiling using GCC as following:

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:

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. The same error occours when trying to develop a python module using Python C API, compiling as shared library.

I also tried compiling the whole thing using distutils/setuptools, the error is kilometric, but the substance is the same as the above one.

The above exception was obtained using this demo code.

Output of 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

CodePudding user response:

This command: gcc file.o -o a.out does not link to a python library.

You need to add (append) -lpython3 and possibly -L${CONDA_PREFIX}/lib/python3.9 to it.

CodePudding user response:

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.

By the way, I imagine Conda comes with some configuration tool that outputs these same options, so you wouldn't have to hardcode the Python version number.

  • Related