pybind / pybind/pybind11

[BUG]: Link Error

Open
#3,595 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
Problem description

How to use my own link library in pybind11??

here is the example code:

test1.h
int add(int a, int b);

test1.cpp

int add(int a, int b){
    return a+b;
}

test2.cpp

#include "test1.h"
#include <pybind11/pybind11.h>
namespace py = pybind11;
int add2(int a, int b){
    return add(a, b)+add(a,b);
}
PYBIND11_MODULE(example, m) {
    m.doc() = "test extra link";
    m.def("add2", &add2, "A function which adds two numbers");
}

firstly, I make test1 to a link library. Then, I use setup.py to make the module.

setup.py

from pybind11.setup_helpers import Pybind11Extension, build_ext
import setuptools
library_dirs = ["./"]
def get_extensions():
    ext_modules = [Pybind11Extension('example', 
        ['test2.cpp'], 
        extra_compile_args=['-Xlinker','-export-dynamic'],
        extra_link_args = ['-L{} -ltest1'.format(library_dirs[0])],
        cxx_std=11),]
    return ext_modules

setuptools.setup(
    name='example',
    ext_modules=get_extensions(),
    cmdclass={'build_ext': build_ext},
    classifiers=["Programming Language :: Python :: 3", "Operating System :: OS Independent"],
    packages=setuptools.find_packages(),
)

here is the Error:
1640165729(1)

when I ldd the .so get this:
1640165790(1)

It seems that didn't link the test1.so

Reproducible example code

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with setup.py and the test1.cpp/test2.cpp example, then inspect the extension build and link command and compare it with the reported ldd output. Determine whether the issue indicates a pybind11 or build-helper problem and document the supported configuration or a reproducible correction; done means the example links and loads with its external library.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.