Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
v-volatus opened this issue May 30, 2022 · 4 comments
Labels
3.10 docs Documentation in the Doc dir expert-C-API

Comments

@v-volatus
Copy link

v-volatus commented May 30, 2022

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

@v-volatus v-volatus added the docs Documentation in the Doc dir label May 30, 2022
@v-volatus v-volatus changed the title Embedding Python in C++: Compiling and Linking under Ubuntu 22.04 Undefined References when using recommended linking option: Embedding Python in C++ (Ubuntu 22.04) May 30, 2022
@v-volatus v-volatus changed the title Undefined References when using recommended linking option: Embedding Python in C++ (Ubuntu 22.04) Undefined References when using recommended linking option: Embedding Python in C++ May 30, 2022
@TianmingQu

This comment was marked as off-topic.

@erlend-aasland
Copy link
Contributor

erlend-aasland commented Oct 25, 2022

Adding the option --embed solves the issue. By this, -lpython3.10 is also given to the compiler.

Yes, the docs are out of date. This was changed in Python 3.8 -- see the 3.8 What's New:

https://docs.python.org/3/whatsnew/3.8.html#debug-build-uses-the-same-abi-as-release-build

Adding --embed when producing the linker flags should help. I'll create a PR for updating the docs.

@erlend-aasland

This comment was marked as off-topic.

erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue Oct 25, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 29, 2022
…nGH-98649)

(cherry picked from commit e063c23)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 29, 2022
…nGH-98649)

(cherry picked from commit e063c23)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
miss-islington added a commit that referenced this issue Oct 29, 2022
(cherry picked from commit e063c23)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
miss-islington added a commit that referenced this issue Oct 29, 2022
(cherry picked from commit e063c23)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 docs Documentation in the Doc dir expert-C-API
Projects
None yet
Development

No branches or pull requests

4 participants