0

I'm trying to embed Python 3.7 in a C application in Windows 10.

#define PY_SSIZE_T_CLEAN
#include <Python.h>

int main () {
    Py_Initialize();
    PyRun_SimpleString("print('OK')");
}

I use the following command to compile: (MinGW-W64-builds-4.3.4, gcc 7.3.0)

gcc "-IC:/Program Files/Python37_64/include" "-LC:/Program Files/Python37_64/libs" -lpython37 main.c

But it gives the following error:

C:\Users\Paul\AppData\Local\Temp\ccKQF3zu.o:main.c:(.text+0x10): undefined reference to `__imp_Py_Initialize'
C:\Users\Paul\AppData\Local\Temp\ccKQF3zu.o:main.c:(.text+0x25): undefined reference to `__imp_PyRun_SimpleStringFlags'
collect2.exe: error: ld returned 1 exit status

The strange thing is, when I try the same in Go 1.13 (Golang), it does work:

package main

/*
#cgo CFLAGS: "-IC:/Program Files/Python37_64/include"
#cgo LDFLAGS: "-LC:/Program Files/Python37_64/libs" -lpython37
#define PY_SSIZE_T_CLEAN
#include <Python.h>

void run () {
    Py_Initialize();
    PyRun_SimpleString("print('OK')");
}

*/
import "C"

func main () {
    C.run()
}

Compile command:

go build python.go

How to fix this?

user42723
  • 467
  • 3
  • 8

1 Answers1

0

I found the solution in this answer.

The argument main.c has to be put somewhere before -lpython37.

So this works:

gcc "-IC:/Program Files/Python37_64/include" "-LC:/Program Files/Python37_64/libs" main.c -lpython37 
user42723
  • 467
  • 3
  • 8