【问题标题】:How to include "<>" cascading headers如何包含“<>”级联标题
【发布时间】:2021-02-25 15:55:31
【问题描述】:

我正在尝试使用 SWIG 来将 C++ 代码用作 Go 模块。 我的文件夹是这样组织的:

MyLib/
   -go-integration
      -basic_host
           -basic_host.i
           -basic_host_wrap.cxx // gen by SWIG
           -basic_host_wrap.h // gen by SWIG
           -basic_host.go // gen by SWIG
   -MyLib/
      -include/
          -mylib
             -common.hpp
             -inputs.hpp
             -guests.hpp
             -logger.hpp
             - ...
      -src/
         -guests.cpp
         -logger.cpp
         - ...
          

我的 basic_host.i 文件:

%module(directors="1") basic_host
%{
    #include <atomic>
    #include <map>
    #include <iostream>

    #include "../../MyLib/include/mylib/inputs.hpp"
    #include "../../MyLib/include/mylib/common.hpp"
    #include "../../MyLib/include/mylib/guests.hpp"
%}


%inline %{
 somefunc to export ....
%}

%constant someConstant...

现在我一直在使用的是:

swig -go -cgo -c++ -intgosize 64 .\basic_host.i
go install

In file included from basic_host_wrap.cxx:344:
../../MyLib/include/mylib/common.hpp:6:10: fatal error: mylib/inputs.hpp: No such file or directory
    6 | #include <mylib/inputs.hpp>
      |   
^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

我的问题是我似乎无法导入我的 C++ 文件,因为它们正在使用“”导入文件。但是在 C++ 方面,我使用这些文件没有问题(导入做得很好)。

我的 go 包需要在 guests.hpp 上导入,但它导入了 common.hpp,它导入了 inputs.hpp 并给了我这个错误。也许有一种方法可以通过 ext 依赖我的 lib 直接导入而不给出相对路径,但我还没有找到它。

这种行为可能可以通过 SWIG 使用 gcc/g++ 并且不知道在哪里可以找到包含文件但我也不知道如何更改/尝试这一事实来解释。

【问题讨论】:

    标签: c++ go swig


    【解决方案1】:

    这正是我所怀疑的,SWIG 使用 gcc 和 cgo 生成文件,您可以将其添加为环境变量 CGO_CPPFLAGS,即包含文件的路径。

    $env:CGO_CPPFLAGS="-I D:/Repos/MyLib/MyLib/include"
    

    我也尝试过像这样使用 SWIG 的命令 -I :

    swig -go -cgo -c++ -I"D:/Repos/MyLib/MyLib/include" -intgosize 64 .\basic_host.i
    

    但它不能直接工作。

    【讨论】:

    • 这很明显。 -I 只影响 SWIG。您收到的错误来自go。作为替代方案,您可以使用接口文件中的%insert("header") 将内容显式添加到生成的`basic_host_wrap.cxx`,并在此处使用部署标头的目录
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 2019-03-12
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 2019-09-01
    相关资源
    最近更新 更多