I'm having trouble importing Python.h to a C program.
I have 4 files, hellofunc.c
, hellomake.c
, hellomake.h
and Makefile
.
The hellomake.h
, Python.h
and all related Python files are in a include
folder.
And the rest is in the program
folder.
├───include
│ ├───cpython
│ └───internal
├───lib
└───program
The code of hellofunc.c
file is:
#include <stdio.h>
#include <hellomake.h>
void myPrintHelloMake(void) {
printf("Hello makefiles!\n");
return;
}
The code of hellomake.h
is:
void myPrintHelloMake(void);
The code of hellomake.c
is:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <windows.h>
#include <Python.h>
#include <hellomake.h>
int main() {
// call a function in another file
myPrintHelloMake();
Py_Initialize();
PyRun_SimpleString("print('Hello World from Embedded Python!!!')");
Py_Finalize();
return(0);
}
The code of Makefile
is:
IDIR =../include
CC=gcc
CFLAGS=-I$(IDIR)
ODIR=obj
LDIR =../lib
LIBS=-lm
_DEPS = hellomake.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = hellomake.o hellofunc.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
hellomake: $(_OBJ)
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
.PHONY: clean
clean:
rm -f *.o *~ core $(INCDIR)/*~
The error that I got is:
gcc -o hellomake hellomake.o hellofunc.o -I../include -lm
hellomake.o:hellomake.c:(.text+0x14): undefined reference to `_imp__Py_Initialize'
hellomake.o:hellomake.c:(.text+0x2a): undefined reference to `_imp__PyRun_SimpleStringFlags'
hellomake.o:hellomake.c:(.text+0x31): undefined reference to `_imp__Py_Finalize'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:56: hellomake] Error 1