I want to package my python binding
for a c++ library
using pybind11
and upload it to pip
. It's easy to run Cmake
and make
to compile the binding project individually.
However, when I use python setup.py, Cmake throws ld: library not found for -lstdc++
on my Mac Mojave
.
- I've tried
xcode-select --install
And
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
Both of them installed properly.Also, I've tried
sudo python setup.py sdist build
and thenpython setup.py sdist build
, amazingly it did work! But the the same error throws when I download and compile frompip install my-package
.- Strangly, I used anaconda
python3.6
on two Mac, it failed both, I create new conda envpy3.7
, It installed successfully!!! I also used /usr/local/bin/python3 (3.4) and also succeed!!!
My setup.py is like this:
import os
import sys
import pathlib
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext as build_ext_orig
class CMakeExtension(Extension):
def __init__(self, name):
# don't invoke the original build_ext for this special extension
super().__init__(name, sources=[])
class build_ext(build_ext_orig):
def run(self):
for ext in self.extensions:
self.build_cmake(ext)
super().run()
def build_cmake(self, ext):
# example of build args
build_args = ['--config', 'Release', '--', '-j4']
cwd = pathlib.Path().absolute()
build_temp = pathlib.Path(self.build_temp)
build_temp.mkdir(parents=True, exist_ok=True)
os.chdir(str(build_temp))
self.spawn(['cmake', str(cwd)])
if not self.dry_run:
self.spawn(['cmake', '--build', '.'] + build_args)
os.chdir(str(cwd))
setup(
...
install_requires=['numpy', 'pybind11'],
include_package_data=True,
ext_modules=[CMakeExtension('xxx')],
cmdclass={
'build_ext': build_ext,
}
)
which I mostly copied from https://stackoverflow.com/a/48015772/7961269
And my CMakeLists.txt is:
cmake_minimum_required(VERSION 2.8.12)
project(xxx)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++")
find_package(pybind11 REQUIRED)
include_directories(...)
pybind11_add_module(xxx xxx/xxx.cpp)
Hope I can pip install my-package
successfully without any errors