1

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.

  1. I've tried xcode-select --install
  2. And open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg Both of them installed properly.

  3. Also, I've tried sudo python setup.py sdist build and then python setup.py sdist build, amazingly it did work! But the the same error throws when I download and compile from pip install my-package.

  4. Strangly, I used anaconda python3.6 on two Mac, it failed both, I create new conda env py3.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

  • https://stackoverflow.com/search?q=%5Bpybind11%5D+ld%3A+library+not+found+for+%22-lstdc%2B%2B%22 – phd Jun 19 '19 at 18:52
  • For Mojave the setting [should be](https://github.com/ContinuumIO/anaconda-issues/issues/10135#issuecomment-426736042) `10.14`. – phd Jun 19 '19 at 18:52
  • thank you @phd, It did work!!! But is there a way to tell pip automatically set this value if needed? – craig zhang Jun 20 '19 at 14:19
  • No idea. Perhaps not as it's a `clang`'s trick, not `pip`'s. – phd Jun 20 '19 at 15:47

1 Answers1

0

The error suggests that something in the python build is automatically adding -lstdc++ to the compiler flags, but because you've told clang to use -stdlib=libc++ it isn't looking in the directory that contains libstdc++ (which is correct, because you told it not to use libstdc++).

I think the python build system is not compatible with libc++, or you need to find some way to tell it to stop adding -lstdc++ automatically.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521