Matteo Arella
Matteo Arella

Reputation: 33

Reference undefined to functions in python-dev header

I'm trying to embedding some Python code into C; It's the first time I do a thing like that.

Here is the simple code of my first attempt copied by a guide on internet:

#include <Python.h>
void exec_pycode(const char* code)
{
  Py_Initialize();
  PyRun_SimpleString(code);
  Py_Finalize();
}

int main(int argc, char **argv) {
    exec_pycode(argv[1]);
    return 0;
}

So I've installed python3.4-dev package.

Then for having info for the linker I typed:

pkg-config --cflags --libs python3

Then I tried to compile my code:

gcc -std=c99 -o main -I /usr/local/include/python3.4m  -L /usr/local/lib -lpython3.4m main.c

(according the command before)

but this is the result:

/tmp/ccJFmdcr.o: in function "exec_pycode":
main.c:(.text+0xd): reference undefined to "Py_Initialize"
main.c:(.text+0x1e): reference undefined to "PyRun_SimpleStringFlags"
main.c:(.text+0x23): reference undefined to "Py_Finalize"
collect2: error: ld returned 1 exit status

It would seem that there is a problem with linking phase, but I can't understend where is the problem seeing that i've passed to the linker the exact paths of the header and of the library. How can I solve that problem?

Upvotes: 1

Views: 483

Answers (1)

user12205
user12205

Reputation: 2702

Try reordering your compilation command, such that all linking options are specified after your C source files:

gcc -std=c99 -o main -I /usr/local/include/python3.4m main.c \
  -L /usr/local/lib -lpython3.4m

Upvotes: 2

Related Questions