c++ - Cgo 找不到像这样的标准库
问题描述
解决方案
cgo makes it very easy to wrap C with Go, but C++ is a bit different. You have to extern "C"
the functions that you want to make a function-name in C++ have 'C' linkage, otherwise the linker won't see the function. So, the actual problem is in the C++ header file. If you can't change the C++ code because it's a library, you may have to write wrappers (example).
This will compile:
.
├── test.cpp
├── test.go
└── test.hpp
test.hpp
#ifdef __cplusplus
extern "C" {
#endif
int test();
#ifdef __cplusplus
}
#endif
test.cpp
#include <iostream>
#include "test.hpp"
int test() {
std::cout << "Hello, World! ";
return 0;
}
test.go
package main
// #cgo CXXFLAGS: -I/usr/lib/
// #cgo LDFLAGS: -L/usr/lib/ -lstdc++
// #include "test.hpp"
import "C"
func main() {
C.test()
}
Put the files in the same folder,
run go build
Hello, World!
推荐阅读
- tensorflow2.0 - tensorflow2.0中的旋转图像及其关键点标签
- microsoft-graph-api - Microsoft Graph SDK 未在丰富的通知中提供约会/事件的草稿/发送状态
- android - 哪个是颤振更好的做法?
- java - 100% - 40 在 Java 中是如何工作的?
- android - 任务':app:preDebugBuild'的执行失败反应原生android
- css - CSS 类在未设置的地方被拾取
- dialogflow-es - 从 Skype 聊天机器人 Dialogflow 的用户接收文件/图像
- python - 在 postgresql 中取消 plpython 运行时
- unicode - Phantomjs 更改文本符号
- javascript - Bootstrap4:将一些内容放在导航图标的顶部