[英]Distutils build include C++ library with 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]
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.