[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文件报错,信息如下
```go
# 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接口文件
```go
%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);
}
};
```
现在更新了swig的接口文件,class 里面带有指针的函数报"undefined reference"
```go
# 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()'
```
#1
更多评论