'How to include Python.h for python installed in miniforge?

I'm trying to install a tensor flow module written in python that is installed inside the miniforge environment. (I use miniforge because I'm using the m1 mac)

Below is my testing c++ code:

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    if (program == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }
    Py_SetProgramName(program);  /* optional but recommended */
    Py_Initialize();
}

I've tried this command to build my cpp file

g++ -arch arm64 -I/opt/homebrew/Caskroom/miniforge/base/envs/ML/include/python3.9 -o py_port py_port.cpp

however I got these error

g++ -arch arm64 -I/opt/homebrew/Caskroom/miniforge/base/envs/ML/include/python3.9 -o py_port py_port.cpp
Undefined symbols for architecture arm64:
  "_Py_DecodeLocale", referenced from:
      _main in py_port-b72f4b.o
  "_Py_Initialize", referenced from:
      _main in py_port-b72f4b.o
  "_Py_SetProgramName", referenced from:
      _main in py_port-b72f4b.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Did I miss anything?



Solution 1:[1]

Link with python library and library path during compilation like this [modify accordingly]

g++ -I/Users/subrata/.miniconda3/include/python3.9 -L/Users/subrata/.miniconda3/lib -lpython3.9  -Wl,-rpath,/Users/subrata/.miniconda3/lib  pyTest.cpp -o py_port

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 subrata