Distutils构建包含c++库与setup



我试图将glfw链接到我的c++ python扩展,但没有cmake我无法做到。

我在cmake中是这样做的:

cmake_minimum_required(VERSION 3.0)
project(Test)
add_subdirectory(lib/glfw)
add_executable(Test py_extend.cpp)
target_include_directories(Test PRIVATE 
${OPENGL_INCLUDE_DIR}
)
target_link_libraries(Test PRIVATE 
${OPENGL_LIBRARY}
glfw
)

这很好。这是我在distutils中尝试做的:

from distutils.core import setup, Extension
from distutils.ccompiler import CCompiler, new_compiler
our_compiler = new_compiler()
our_compiler.add_include_dir("glfw")
our_compiler.add_library("glfw")
our_compiler.add_library_dir("lib/glfw")
module1 = Extension('test',
sources = ['py_extend.cpp', 'src/glfw_binder.cpp'],
include_dirs=["include/glfw_binder.h"],
libraries=["glfw"],
library_dirs=["lib/glfw"]
)
setup (name = 'python_test',
version = '1.0',
description = 'This is a demo package',
ext_modules = [module1]
)

当我运行这个时,我得到这个错误:

python3 build.py build
running build
running build_ext
building 'test' extension
x86_64-linux-gnu-gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Iinclude/glfw_binder.h -I/usr/include/python3.10 -c py_extend.cpp -o build/temp.linux-x86_64-3.10/py_extend.o
cc1plus: warning: include/glfw_binder.h: not a directory
In file included from py_extend.cpp:4:
./include/glfw_binder.h:4:10: fatal error: GLFW/glfw3.h: No such file or directory
4 | #include <GLFW/glfw3.h>
|          ^~~~~~~~~~~~~~
compilation terminated.
error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1

我如何正确地实现cmake代码到distutils代码?

我把它改成了setuptools(我应该改成它,因为distutils很快就会被弃用了),然后像这样写:

from setuptools import setup, Extension
module1 = Extension('nerveblox_test',
sources = ['py_extend.cpp', 'src/nerveblox_VM.cpp'],
include_dirs=["include/nerveblox_VM.h", "lib/glfw/include"],
libraries=["lib/glfw"],
library_dirs=["lib/glfw"],
extra_objects=["lib/glfw/include"]
)
setup (name = 'Nerveblox_python_test',
version = '1.0',
description = 'This is a demo package',
ext_modules = [module1]
)

最新更新