使用 gcc 而非 g++ 在 golang 中进行构建
时间:2024-02-21
浏览:228
作者:佚名
Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《使用 gcc 而非 g++ 在 golang 中进行构建》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!
问题内容
我正在尝试构建在 linux 上调用 c++ 创建的 .so (a.so) 文件的 go,但我发现 go build . 命令始终使用 gcc 构建不是g++。我已经将 .cpp 放在根目录中而不是子目录中。
这是 go build 命令的输出
client.go:90:10: could not determine kind of name for c.init
cgo:
gcc errors for preamble:
in file included from client.go:8:
a.h:34:1: error: unknown type name 'class'
34 | class a {
| ^~~~~
a.h:34:11: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
34 | class a {
| ^ ^
这是 client.go 调用 c 代码:
package main
// #cgo windows ldflags: -la
// #cgo windows cxxflags: -dwindows
// #cgo linux ldflags: -liba
// #cgo ldflags: -l./libs
// #cgo cxxflags: -i./
// #include "a.h"
import "c"
func function() {
handle, _ := dlopen(path_to_so_file)
blob := c.init(handle)
}
这是 dlopen 相关代码,用 go 编写:
// +build linux
package main
// #cgo linux ldflags: -ldl
// #include <dlfcn.h>
// #include <stdlib.h>
import "c"
import "errors"
import "unsafe"
type handle {
c unsafe.pointer
}
func dlopen(filename string) (handle, error) {
ptr := c.cstring(filename)
defer c.free(unsafe.pointer(ptr))
ret := c.dlopen(ptr, c.rtld_lazy)
if ret != nil {
return handle{ret}, nil
}
return handle{ret}, errors.new(c.gostring(c.dlerror()))
}
这是 a.h
class A {
public:
Init(MHANDLE handle);
}解决方案
您的问题不在于 cpp 文件。
你在go文件中写了 // #include "a.h"。
Go 目前将其编译为 c,并且不支持 c++,而且看起来永远不会支持。
您唯一的选择是使头文件在 c 中有效。
本篇关于《使用 gcc 而非 g++ 在 golang 中进行构建》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.26
11.20
11.20
11.20
热门专题
