如何让 cppyy 找到额外的 headers?

How do I get cppyy to find additional headers?

我有一个目录结构:

include/foo/bar/header1.h
include/foo/bar/header2.h

header1.h 包括 header2.h。但是,当我尝试这样做时:

import cppyy

cppyy.add_include_path('include')
cppyy.include('foo/bar/header1.h')

我收到错误:

ImportError: Failed to load header file "foo/bar/header1.h"
In file included from input_line_33:1:
./include/foo/bar/header1.h:11:10: fatal error: 'foo/bar/header2.h' file not found
#include "foo/bar/header2.h"
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我不知道该做什么。我可以手动包含 header2.h,但它又包含其他包含文件,我可能最终会手动包含整个项目,这似乎不需要发生。 cppyy.include('include/foo/bar/header1.h')cppyy.include('foo/bar/header1.h') 我都试过了。我也试过 cppyy.add_include_path('include')cppyy.add_include_path('include/foo')。这些都没有帮助。

尽管上面的评论中有结论,但以下是为了将来参考以帮助调试,以防有人偶然发现这个问题并遇到同样的问题。

要打印 Cling 看到的完整包含目录集,请执行:

import cppyy
print(cppyy.gbl.gInterpreter.GetIncludePath())

它将显示为一系列 -I<dir> 个参数,就像在命令行上提供给 C++ 编译器一样。

每个的 <dir> 部分中的路径可以是绝对路径或相对路径。如果存在任何相对路径,Cling 将从进程的当前目录考虑它们,因此基本的 os.getcwd() 可以检索它并且可以使用 os.chdir() 来更改它。

如果在程序运行期间的任何时候 运行 当前工作目录可能发生意外更改,那么我建议将所有路径都设为 Cling 绝对路径(或立即包含所有相关 headers)。例如:

cppyy.add_include_path(os.path.abspath('include'))