我已经成功地将皮皮用于C++项目的自动python绑定。我最近包括了本征库,但是我在与cppyy一起使用它时遇到了困难。有没有人有这样的经验,或者知道我该怎么做?
我有以下的回购结构(只显示了相关部分):
.
├── CMakeLists.txt
├── build
├── external
── eigen
├── include
── all .hpp files
├── src
── all .cpp files
├── python
── qmc.py这里,external/eigen是特征GitHub回购的副本。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)构建部分工作,但一旦我试图加载任何使用本征头,我就会得到一个错误。我已经尝试了所有我能想到的东西(一个一个地包括所需的头,将整个库复制到build等等)。但是不管我做什么,都会出现相同类型的错误。有点像
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还需要包含头文件。同样,这也适用于我自己的标题,但它无法找到本征标头.
发布于 2018-12-30 17:42:55
当调用help()时,有:
>>> import cppyy
>>> help(cppyy)
"""
add_include_path(path)
Add a path to the include paths available to Cling.
"""
>>>因此,既然eigen_dir是通往特征的道路,那么这应该是一张票:
cppyy.add_include_path(eigen_dir)但是,有更好的方法,因为您已经在使用cmake了。参见这个回购:https://github.com/jclay/cppyy-knearestneighbors-example。那样的话,自动装载就可以了。也就是说,不需要在自己的代码中处理库和标头。
https://stackoverflow.com/questions/53818771
复制相似问题