【问题标题】:Build C library with CMakeLists.txt while packaging python library打包 python 库时使用 CMakeLists.txt 构建 C 库
【发布时间】:2020-04-08 07:30:51
【问题描述】:

我使用 Cython 来包装 C 库并扩展其功能。 C 库由单独的团队开发,并随 CMakeLists.txt 提供。现在我使用源 .c 和 .h 文件来构建这个库。这就是我的build.py 诗歌文件(setup.py 模拟)的样子:

...
# source_files_paths - list of full paths to .c files
c_library = ('c_library', {'sources': source_files_paths})
...
def build(setup_kwargs):
    setup_kwargs.update({
        ...,
        'libraries': [c_library],  # Here c library is built with build_clib command
        ...
    })

是否可以在build函数(或setuptools.setup函数)中使用CMakeLists.txt构建c_library

UPD:我也可以定义 .c 文件中使用的变量。我尝试添加define_macros

c_library = ('c_library', {'sources': source_files_paths,
                           'define_macros': [('MY_MACRO': True)]})

仍然没有运气。

UPD2:我终于找到了解决问题的方法,尽管这不是我最初问题的答案。要添加宏,您应该以这种方式添加它:

c_library = ('c_library', {'sources': source_files_paths,
                           'macros': [('MY_MACRO': True)]})

根据http://svn.python.org/projects/python/branches/pep-0384/Lib/distutils/command/build_clib.py 中的build_libraries 函数,您还可以指定“include_dirs”、“obj_deps”和“cflags”参数。

【问题讨论】:

  • 在工作中,我编写了一些脚本来使用 python 构建我的 cmake 项目。我能够使用 python 中的 subprocess 模块来做到这一点。这让我可以轻松地从 python 构建我的 cmake 项目。这是你想要的吗?

标签: python c cmake cython setup.py


【解决方案1】:

scikit-build 包可以替代 setuptools 并且可以使用 CMakeLists.txt。 (见Basic Usage guide for scikit-build)。可以CMake with Cython.

【讨论】:

  • 我试过了,但找不到用它构建底层 C 库的方法。据我所知,它只能构建 Cython 包 cmakefile,而不是 cmake 文件链。如果我错了,你能给我举个例子吗?
  • 此外,找不到如何将它与诗歌一起使用
  • 将 C 库的 CMakeLists 指令直接添加到用于构建 Cython 代码的 CMakeLists.txt 时会发生什么?尝试手动包含此文本,或使用指令include
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
  • 2015-10-01
  • 1970-01-01
  • 2012-04-19
  • 2010-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多