Binding generation issue on ubuntu16.04

Issue #76 resolved
Former user created an issue


I tried to use cppyy to generate python binding module like On py2.7 there's no error during building. However, when I try to use the binding module, there's something that doesn't work very well.

from cppyy.gbl import std
import knn_example
from knn_example import NearestNeighbors, Point

Point.__repr__ = lambda self: repr(str(self.x) + ", " + str(self.y))

knn = NearestNeighbors()
points = [Point(2,0), Point(1,0), Point(0,10), Point(5,5), Point(2,5)]
knn.points = std.vector[Point](points)
result = knn.nearest(Point(2.1, 5.3), 4)

I try to run this script, but in

result = knn.nearest(Point(2.1, 5.3), 4)

I got the following error.

IncrementalExecutor::executeFunction: symbol '_ZN16NearestNeighbors7nearestE5Pointi' unresolved while linking symbol '__cf_9'!
You are probably missing the definition of NearestNeighbors::nearest(Point, int)
Maybe you need to load the corresponding shared library?
Error in <TClingCallFunc::make_wrapper>: Failed to compile
  ==== SOURCE BEGIN ====
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-security"
__attribute__((used)) extern "C" void __cf_9(void* obj, int nargs, void** args, void* ret)
   if (ret) {
      new (ret) (vector<Point>) (((NearestNeighbors*)obj)->nearest(*(Point*)args[0], *(int*)args[1]));
   else {
      ((NearestNeighbors*)obj)->nearest(*(Point*)args[0], *(int*)args[1]);
#pragma clang diagnostic pop
  ==== SOURCE END ====
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: vector<Point> NearestNeighbors::nearest(Point, int k) =>
    ValueError: nullptr result where temporary expected

It's weird that I need to explictly load the "knn_lib" library to avoid that error.

Maybe the LINK_LIBRARIES argument in cppyy_add_bindings() of CMakeLists.txt didn't work?

Here is the environment:


Ubuntu 16.04 (Windows 10 subsystem)


cppyy (1.4.5)

cppyy-backend (1.7.4)

cppyy-cling (

CPyCppyy (1.6.4)

Comments (7)

  1. Lycen Yao

    I forgot to sign in when I post this issue. Thanks for your reply, wlav. The cppyy-bbhash example looks nice, especially the selection file part.

    Edit: The bbhash example works fine only when the whole library is header-only. Otherwise calling methods still throws missing definition error

  2. Camille Scott

    I now have a working version for the knn example. The issue was more or less that CMake needs to be given the LINK_WHAT_YOU_USE directive for linking the cppyy lib against the c++ shared lib; otherwise, ld doesn't detect that the cppyy lib needs those symbols, and it drops the shared lib (LINK_WHAT_YOU_USE controls the --as-needed/--no-as-needed gcc/ld flags). As you can see, this is all working under continuous integration now as well, so portability is good.

    Cheers, Camille

  3. Log in to comment