Skip to content

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

Closed

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

Activity

  1. 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)[/+] on May 30, 2022
  2. 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++ [/+] on May 30, 2022
  3. TianmingQu commented on Oct 24, 2022

    @TianmingQu
  4. erlend-aasland commented on Oct 25, 2022

    @erlend-aasland
    Contributor

    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.

  5. erlend-aasland commented on Oct 25, 2022

    @erlend-aasland
  6. added 2 commits that reference this issue on Oct 25, 2022
  7. added 2 commits that reference this issue on Oct 29, 2022
  8. added 2 commits that reference this issue on Oct 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions