go通过swig方式调用c++,遇到困难了,有偿求助!

18393910396 · 2021-09-18 20:14:29 · 2323 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2021-09-18 20:14:29 的主题,其中的信息可能已经有所发展或是发生改变。

[toc]

1: 现有资料

  • cpp头文件
  • 一个.so库
  • cpp头文件API说明文档

2:现在进度

  • 基本完成swig接口文件,已经可以进行swig -c++ -go -cgo -intgosize 64 ./xxx.i进行转换
  • 生成 xxx_wrap.h,xxx_wrap.cxx,xxx.go
  • go build进行编译的时候,xxx_wrap.cxx文件报错,信息如下
    # goama/src
    ama_wrap.cxx: In function ‘_gostring_ _wrap_CPPIAMDApi_GetVersion_src_339c721e06099bce():
    ama_wrap.cxx:21967:24: error: ‘CPPIAMDApi_GetVersion’ was not declared in this scope
    21967 |       result = (char *)CPPIAMDApi_GetVersion();;
    ...
    ama_wrap.cxx:21960:11: error: ‘CPPIAMDSpi’ in namespace ‘amd::ama’ does not name a type; did you mean ‘IAMDSpi’?
    21960 | amd::ama::CPPIAMDSpi *_wrap_new_CPPIAMDSpi_src_9218e0d178690c28() {
        |           ^~~~~~~~~~
        |           IAMDSpi
    ... 
    很多类似不在scope,找不见对应的class等的错误
    

    3:遇到的困难

    1: swig 转Go的时候,带有指针的函数如何处理,目前是class放在swig交换文件中 2:cpp 的namespace如何处理,现在的问题好像是这个原因导致的 3: 或者有其他更好的思路

    4:补充

    4.1 关键点

  • namespace
  • reference
  • class

    4.2 swig接口文件

%module(directors="1") src

%{
    #include <vector>
    #include <stdint.h>
    #include <string>
    #include "ama_datatype.h"
    #include "ama_export.h"
    #include "ama_struct.h"
    #include "ama_tools.h"
    #include "ama.h"
%}

%include "ama_datatype.h"
%include "ama_export.h"
%include "ama_struct.h"
%include "ama_tools.h"
%include "ama.h"


%include "std_deque.i"
%include "std_pair.i"
%include "std_string.i"
%include "std_vector.i"
%include "stdint.i"
%include "typemaps.i"

// %include "std_multimap.i"
// %include "std_multiset.i"
// %include "std_set.i"
// %include "std_array.i"
// %include "std_auto_ptr.i"
// %include "std_complex.i"
// %include "std_unordered_map.i"
// %include "std_unordered_multimap.i"
// %include "std_unordered_multiset.i"
// %include "std_unordered_set.i"
// %include "std_wstring.i"
// %include "std_shared_ptr.i"

%feature("director")IAMDSpi;

// typemap
%typemap(gotype) unsigned int "uint";



// transform c++ exception into go errors
%include "exception.i"
%exception {
    try {
        $action;
    } catch (std::runtime_error &e) {
        _swig_gopanic(e.what());
    }
}

%rename(CPPIAMDApi) amd::ama::IAMDApi;
%rename(CPPIAMDSpi) amd::ama::IAMDSpi;

// namespace 
%nspace amd::ama::IAMDSpi;
%nspace amd::ama::IAMDApi;
%nspace amd::ama::CPPIAMDApi;
%nspace amd::ama::CPPIAMDSpi;
%nspace amd::ama::Tools;

// // //
// // %nodefaultctor amd::ama::IAMDApi;
// // %nodefaultctor amd::ama::IAMDSpi;

// // 


class CPPIAMDApi {
   public:
    %extend{
        static const char* GetVersion();
        static int32_t Init(const IAMDSpi* pSpi, const Cfg& cfg);
        static void Join();
        static int32_t Release();
        static void FreeMemory(void* data);
        static int32_t SubscribeData(int32_t subscribe_type, const SubscribeItem* item, uint32_t cnt);
        static int32_t SubscribeData(int32_t subscribe_type, const SubscribeCategoryItem* item, uint32_t cnt);
        static bool GetCodeTableList(CodeTableRecordList& list);
    }
};

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2323 次点击  ∙  1 赞  
加入收藏 微博
3 回复  |  直到 2022-07-19 20:50:15
18393910396
18393910396 · #1 · 4年之前

现在更新了swig的接口文件,class 里面带有指针的函数报"undefined reference"

# goama/src
/usr/bin/ld: $WORK/b038/_x003.o: in function `_wrap_IAMDApi_GetVersion_src_07cd417a0356dfef':
src/ama_wrap.cxx:21338: undefined reference to `amd::ama::IAMDApi::GetVersion()'
/usr/bin/ld: $WORK/b038/_x003.o: in function `_wrap_IAMDApi_Init_src_07cd417a0356dfef':
src/ama_wrap.cxx:21353: undefined reference to `amd::ama::IAMDApi::Init(amd::ama::IAMDSpi const*, amd::ama::Cfg const&)'
/usr/bin/ld: $WORK/b038/_x003.o: in function `_wrap_IAMDApi_Release_src_07cd417a0356dfef':
src/ama_wrap.cxx:21370: undefined reference to `amd::ama::IAMDApi::Release()'
18393910396
18393910396 · #2 · 4年之前

解决了,是共享库的问题。

18393910396
18393910396 · #3 · 3年之前

swig 对lock ,atomic 等同步控制不能支持。lock不能被copy

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传