Skip to content

Undefined References when using recommended linking option: Embedding Python in C++  #93358

@v-volatus

Description

@v-volatus

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10only security fixesdocsDocumentation in the Doc dirtopic-C-API

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions