背景
最近业务上需要复用CPP编写的客户端SDK库,为了让团队主力语言Golang能够顺利接入SDK,因此使用了CGO桥接技术将C++11编写的SDK库封装成生产环境可用的Golang SDK,在翻阅了网上大部分关于cgo的中英文资料后,发现其中尤其是实现Go调用CPP库还是有非常多需要注意的细节,大部分中文资料都是以简单的C++的STL库函数封装为例点到为止,本文在前人基础之上总结了一系列封装复杂CPP库时的最佳实践的tips,希望能填补相关资料的空白。
最近业务上需要复用CPP编写的客户端SDK库,为了让团队主力语言Golang能够顺利接入SDK,因此使用了CGO桥接技术将C++11编写的SDK库封装成生产环境可用的Golang SDK,在翻阅了网上大部分关于cgo的中英文资料后,发现其中尤其是实现Go调用CPP库还是有非常多需要注意的细节,大部分中文资料都是以简单的C++的STL库函数封装为例点到为止,本文在前人基础之上总结了一系列封装复杂CPP库时的最佳实践的tips,希望能填补相关资料的空白。
Best Practices for Wrapping CPP Library with CGO
Recently, our business needed to reuse a client SDK library written in CPP. To enable our team’s primary language, Golang, to seamlessly integrate with this SDK, we used CGO bridging technology to wrap the C++11 SDK library into a production-ready Golang SDK. After reviewing most of the online resources about CGO in both Chinese and English, I found there are many important details to consider when implementing Go calls to CPP libraries. Most Chinese materials only briefly mention simple examples of wrapping C++ STL library functions. Building on previous work, this article summarizes a series of best practices for wrapping complex CPP libraries, aiming to fill the gap in related documentation.