0

I am running Debian 8 with a packaged install of Cython (apt-get install cython).

I am compiling my .pyx file with the CGAL (www.cgal.org) but return the error:

import pyximport; pyximport.install()
from spaces import spaces_rectangle

ImportError: Building module spaces failed: ['ImportError: /home/scootie/.pyxbld/lib.linux-x86_64-2.7/spaces.so: undefined symbol: __gmpq_equal\n']

with the following files:

spaces.pyx

from libcpp.vector cimport vector

cdef extern from "cgal_spaces.hpp":
    cdef vector[vector[vector[double]]] wrap_spaces(vector[vector[double]])

def spaces_rectangle(vector[vector[double]] rect):
    return wrap_spaces(rect)

spaces.pyxbld:

def make_ext(modname, pyxfilename):
    from distutils.extension import Extension
    return Extension(name=modname,
                     sources=[pyxfilename],
                     include_dirs=['.'],
                     libraries=['CGAL'],
                     language='c++',
                     extra_compile_args=['-std=c++11'])

and cgal_spaces.hpp:

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Partition_traits_2.h>
#include <CGAL/Partition_is_valid_traits_2.h>
#include <CGAL/polygon_function_objects.h>
#include <CGAL/partition_2.h>
#include <cassert>
#include <list>
#include <vector>
{
    *CODE HERE*
}

Am I linking improperly or missing something obvious??

Edit: If I compile the script outside of pyximport, it compiles with no problem.

cython -a spaces.pyx
g++ -std=c++11 -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.7 -o spaces.so spaces.c

It seems there's a linking error in the pyximport with the gmp library. What's the proper way to linking to all external libraries?

scootie
  • 43
  • 5
  • Possible duplicate of [Why does the order in which libraries are linked sometimes cause errors in GCC?](http://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc) – n. m. could be an AI Aug 21 '16 at 15:50
  • This was exactly it!! I've added an additional edit to my main post to describe the solution. Easy as pie, thanks :) – scootie Aug 21 '16 at 18:53

2 Answers2

1

Solution:

def make_ext(modname, pyxfilename):
    from distutils.extension import Extension
    return Extension(name=modname,
                 sources=[pyxfilename],
                 include_dirs=['.'],
                 libraries=['CGAL','gmp'],
                 language='c++',
         extra_compile_args=['-std=c++11','-DCGAL_ROOT="/path/to/CGAL-4.8.1"'])

I've added the gmp library to *.pyxbld, but the solution lies in placing the -DCGAL_ROOT after "-std=c++11".

scootie
  • 43
  • 5
  • You are essentially repeating a part of my answer, "gmp library may be missing from libraries...". That `-DCGAL_ROOT="/path/to/CGAL-4.8.1"` is rather odd and should be explained in more detail. I doubt it affects the compilation process. – J.J. Hakala Aug 22 '16 at 07:24
  • 1
    After adding the 'gmp' to the libararies, it still did not compile. It only compiled after I installed CGAL from source and linked to the installed path. Also, this seemed to be a Debian 8 issue. It compiled without any errors on Fedora20. – scootie Aug 22 '16 at 18:08
0

gmp library may be missing from libraries,

strings -f /usr/lib/x86_64-linux-gnu/*.a |grep gmpq_equal

outputs

/usr/lib/x86_64-linux-gnu/libgmp.a: __gmpq_equal
/usr/lib/x86_64-linux-gnu/libgmp.a: __gmpq_equal
J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
  • I return the same output suggesting that the library is there: `strings -f /usr/lib/x86_64-linux-gnu/*.a | grep gmpq_equal /usr/lib/x86_64-linux-gnu/libgmp.a: __gmpq_equal /usr/lib/x86_64-linux-gnu/libgmp.a: __gmpq_equal` I've installed CGAL both from source and through apt and it compiles fine outside of pyximport. – scootie Aug 21 '16 at 14:25