-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Description
Documentation
URL: https://docs.python.org/3/extending/embedding.html
System: Ubuntu 22.04 using Python 3.10
Problem:
Using the recommended linking option /opt/bin/python3.10-config --ldflags
leads to undefined references when compiling.
command used in terminal:
g++ $(python3.10-config --cflags ) -o 'test.o' 'test.cpp' $(python3.10-config --ldflags)
Output of python3.10-config --ldflags
:
-L/usr/lib/python3.10/config-3.10-x86_64-linux-gnu -L/usr/lib/x86_64-linux-gnu -lcrypt -ldl -lm -lm
Solution:
Adding the option --embed
solves the issue. By this, -lpython3.10
is also given to the compiler.
command used in terminal:
g++ $(python3.10-config --cflags ) -o 'test.o' 'test.cpp' $(python3.10-config --embed --ldflags)
output of python3.10-config --embed --ldflags
:
-L/usr/lib/python3.10/config-3.10-x86_64-linux-gnu -L/usr/lib/x86_64-linux-gnu -lpython3.10 -lcrypt -ldl -lm -lm
test.cpp:
#include < Python.h >
int main()
{
Py_Initialize();
PyRun_SimpleString("print('Hello World from Embedded Python!!!')");
Py_Finalize();
return 0;
}