如何包含“<>”级联标题

huangapple go评论91阅读模式
英文:

How to include "<>" cascading headers

问题

我正在尝试使用SWIG将C++代码作为Go模块使用。
我的文件夹组织如下:

MyLib/
   -go-integration
      -basic_host
           -basic_host.i
           -basic_host_wrap.cxx // 由SWIG生成
           -basic_host_wrap.h // 由SWIG生成
           -basic_host.go // 由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 %{
 导出的一些函数....
%}

%constant 一些常量...

我目前使用的命令是:

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

在文件 basic_host_wrap.cxx 的第344行包含的文件中:
../../MyLib/include/mylib/common.hpp:6:10: 致命错误: mylib/inputs.hpp: 没有那个文件或目录
    6 | #include <mylib/inputs.hpp>
      |   
^~~~~~~~~~~~~~~~~~~~~~
编译终止。

我的问题是,似乎我无法导入我的C++文件,因为它们使用了“<>”符号导入文件。然而,在C++端使用这些文件时没有问题(导入成功)。

我需要在guests.hpp中导入我的go包,但它导入了common.hpp,而common.hpp又导入了inputs.hpp,从而导致了错误。也许有一种方法可以直接通过外部依赖项导入我的库,而不是使用相对路径,但我还没有找到这种方法。

这种行为可能可以解释为SWIG使用gcc/g++,并且不知道如何找到包含文件的位置,但我也不知道如何更改/尝试。

英文:

I'm trying to use SWIG to be able to use C++ code as a Go module.
My folders are organized as this:

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
         - ...

My basic_host.i file:

%module(directors=&quot;1&quot;) basic_host
%{
    #include &lt;atomic&gt;
    #include &lt;map&gt;
    #include &lt;iostream&gt;

    #include &quot;../../MyLib/include/mylib/inputs.hpp&quot;
    #include &quot;../../MyLib/include/mylib/common.hpp&quot;
    #include &quot;../../MyLib/include/mylib/guests.hpp&quot;
%}


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

%constant someConstant...

Right now what I have been using is:

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 &lt;mylib/inputs.hpp&gt;
      |   
^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

My problem is that it seems that I'm unable to import my C++ files as they are importing files with "<>". However on C++ side I've no problem for using those files(the importing is done well).

My import for my go package is needed on guests.hpp but it imports common.hpp that imports inputs.hpp and giving me that error. Maybe there is a way to import direclty by ext dependencies my lib and not giving the relative path but I haven't found it.

This behaviour might be explainable by the fact that SWIG is using gcc/g++ and doesn't know where to find include files but I don't know either how to change/try that.

答案1

得分: 0

这是我怀疑的情况,SWIG使用gcc和cgo来生成文件,你可以将CGO_CPPFLAGS作为环境变量添加,它是指向你的包含文件路径的路径。

$env:CGO_CPPFLAGS=&quot;-I D:/Repos/MyLib/MyLib/include&quot;

我还尝试了使用SWIG的-I命令,像这样:

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

但它没有直接起作用。

英文:

This is what I was suspecting, SWIG uses gcc and cgo to produce the files and you can add as an environment variable CGO_CPPFLAGS that is the path to your include files.

$env:CGO_CPPFLAGS=&quot;-I D:/Repos/MyLib/MyLib/include&quot;

I also tried with the command -I of SWIG like this :

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

But it wasn't working direclty.

huangapple
  • 本文由 发表于 2021年2月25日 23:55:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/66371922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定