'Problems with CPython on MacOS and Apple M1 chip

I recently moved my content to a new Apple M1 laptop running macOS Monterey and I have some problems with a very simple piece of code that tries to call a Python function from C++. A concrete example is available here.

I installed a new version of Python 3.9.10 via Homebrew, but it seems it is not being detected by my compiler since I get the following errors:

surrutiaquir$ g++ -o main_ctp_fN_grid ctp_fN_grid.cpp -lgsl -lm -lgslcblas -std=c++11
Undefined symbols for architecture arm64:
  "_PyDict_GetItemString", referenced from:
      _main in ctp_fN_grid-2fc2a2.o
  "_PyImport_Import", referenced from:
      _main in ctp_fN_grid-2fc2a2.o
  "_PyImport_ImportModule", referenced from:
      _main in ctp_fN_grid-2fc2a2.o
  "_PyList_Append", referenced from:
      _main in ctp_fN_grid-2fc2a2.o
  "_PyModule_GetDict", referenced from:
      _main in ctp_fN_grid-2fc2a2.o
  "_PyObject_CallObject", referenced from:
      _main in ctp_fN_grid-2fc2a2.o
  "_PyObject_GetAttrString", referenced from:
      _main in ctp_fN_grid-2fc2a2.o
  "_PyString_FromString", referenced from:
      _main in ctp_fN_grid-2fc2a2.o
  "_Py_Finalize", referenced from:
      _main in ctp_fN_grid-2fc2a2.o
  "_Py_Initialize", referenced from:
      _main in ctp_fN_grid-2fc2a2.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I'm really confused about this issue, so any guidance would be highly appreciated. Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source