Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling Cython with Xcode 10

I have tried to compile a simple example with Cython and got this linker error (macOS, Xcode 10):

gcc -fno-strict-aliasing -I/anaconda2/envs/python2.7-base/include -arch x86_64 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/anaconda2/envs/python2.7-base/include/python2.7 -c noway.cpp -o build/temp.macosx-10.6-x86_64-2.7/noway.o -std=c++11 -Os -stdlib=libc++ -mmacosx-version-min=10.7
g++ -bundle -undefined dynamic_lookup -L/anaconda2/envs/python2.7-base/lib -arch x86_64 -arch x86_64 build/temp.macosx-10.6-x86_64-2.7/one.o build/temp.macosx-10.6-x86_64-2.7/noway.o -L/anaconda2/envs/python2.7-base/lib -o /Users/dkotsur/Projects/InCeM/IF-MedialAxis/test/noway.so
clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9 [-Wdeprecated]
ld: library not found for -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'g++' failed with exit status 1

My anaconda environment is this:

Python 2.7.15 |Anaconda custom (64-bit)| (default, May  1 2018, 18:37:05) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin

It seems like anaconda uses libstdc++ instead of libc++ for Mac. But I have no clues how to fix it.

Had anyone have something similar? Any clues on how to deal with that?

Here is my code setup.py

if platform.system() == "Windows":
    extra_args = ["/std:c++latest", "/EHsc"]
elif platform.system() == "Darwin":
    extra_args = ['-std=c++11', "-Os", "-stdlib=libc++", "-mmacosx-version-min=10.7"]
else:
    extra_args = []

compile_files = [
    "one.pyx",
    "noway.cpp",
]

include_paths = [
]

ext_modules = [Extension("noway", compile_files,
                         language='c++',
                         include_dirs=include_paths,
                         extra_compile_args=extra_args)]

setup(cmdclass={'build_ext': build_ext}, ext_modules=cythonize(ext_modules))

one.pyx:

import cython

cdef extern from "noway.hpp":
    cdef void noway();


def nway():
    noway()

noway.hpp

#ifndef noway_h
#define noway_h

void noway();

#endif

noway.cpp

#include "noway.hpp"
#include <iostream>

void noway() {
    std::cout << "No way!!!" << std::endl;
}
like image 228
dm_k Avatar asked Sep 22 '18 21:09

dm_k


1 Answers

Thanks, @ead for the comment. You helped a lot.

I have fixed setup.py by adding -stdlib=stdc++ to extra link args instead of compile args. I also had to add -mmacosx-version-min=10.9 to the extra link args.

So, setup.py looks now like this:

import platform
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize

compile_extra_args = []
link_extra_args = []

if platform.system() == "Windows":
    compile_extra_args = ["/std:c++latest", "/EHsc"]
elif platform.system() == "Darwin":
    compile_extra_args = ['-std=c++11', "-mmacosx-version-min=10.9"]
    link_extra_args = ["-stdlib=libc++", "-mmacosx-version-min=10.9"]


compile_files = [
    "one.pyx",
    "noway.cpp",
]

ext_modules = [Extension("noway", compile_files,
                         language='c++',
                         extra_compile_args=compile_extra_args,
                         extra_link_args=link_extra_args)]

setup(cmdclass={'build_ext': build_ext}, ext_modules=cythonize(ext_modules))
like image 78
dm_k Avatar answered Sep 21 '22 00:09

dm_k