[英]Use the Eigen library with cppyy
我已经成功地使用cppyy为我正在研究的C ++项目进行自动python绑定。 我最近加入了Eigen库,但是我很难和cppyy一起使用它。 有没有人有这方面的经验,或者知道我应该怎么做?
我有回购的以下结构(仅显示相关部分):
.
├── CMakeLists.txt
├── build
├── external
── eigen
├── include
── all .hpp files
├── src
── all .cpp files
├── python
── qmc.py
这里external/eigen
是Eigen GitHub repo的副本。 qmc.py
文件是cppyy魔法发生的地方,它看起来像这样(在尝试添加Eigen之前,这个工作正常)
import cppyy
import tempfile
import os
import glob
try:
current_dir = os.path.dirname(__file__)
except NameError:
current_dir = os.getcwd()
source_dir = os.path.dirname(current_dir)
install_dir = os.path.join(source_dir, 'build')
include_dir = os.path.join(source_dir, 'include')
eigen_dir = os.path.join(source_dir, 'external', 'eigen')
print(current_dir, source_dir, include_dir, install_dir)
def cmake_run(build_type='Release', c_compiler='gcc', cxx_compiler='g++'):
os.environ['CC'] = c_compiler
os.environ['CXX'] = cxx_compiler
os.system('cd {} && cmake {} -DCMAKE_BUILD_TYPE={}'.format(install_dir, source_dir, build_type))
def load_library():
os.system('cd {} && make engine'.format(install_dir))
libraries = glob.glob(os.path.join(install_dir, 'libengine.*'))
print('Found libraries: {}'.format(libraries))
library = libraries[0]
cppyy.load_library(library)
for header in glob.glob(os.path.join(include_dir, '*.hpp')):
print('Loading {}'.format(header))
cppyy.include(header)
构建部分工作,但是一旦我尝试加载任何使用Eigen的头,我就会收到错误。 我已经尝试了所有我能想到的东西(逐个手动包含所需的标题,将整个库复制到构建目录等)但是无论我做什么,都会弹出相同类型的错误。 就像是
In file included from
/path/to/repo/projects/include/myheader.hpp:3:10: fatal error: 'Eigen/Dense' file not found
#include <Eigen/Dense>
^~~~~~~~~~~~~
任何有关如何改变的帮助将不胜感激!
编辑 :要清楚,构建步骤工作得很好,即代码编译,链接和运行应该。 使用cppyy加载库也有效。 问题是cppyy还需要包含头文件。 同样,这适用于我自己的标题,但它无法找到Eigen标题...
调用help()时,有:
>>> import cppyy
>>> help(cppyy)
"""
add_include_path(path)
Add a path to the include paths available to Cling.
"""
>>>
因此,当eigen_dir
是Eigen的路径时,这应该是票证:
cppyy.add_include_path(eigen_dir)
但是,有更好的方法,因为你已经使用了cmake。 请参阅此repo: https : //github.com/jclay/cppyy-knearestneighbors-example 。 有了这个,自动加载应该工作。 也就是说,不需要在自己的代码中处理库和头文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.