[BUG]: Link Error
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
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:

when I ldd the .so get this:

It seems that didn't link the test1.so
Reproducible example code
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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