gpt4 book ai didi

go - 使用gcc而不是g++构建golang

转载 作者:行者123 更新时间:2023-12-01 22:33:50 28 4
gpt4 key购买 nike

我试图构建在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 {
| ^ ^
这是调用C代码的 client.go:
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)
}
这是用Go编写的dlOpen相关代码:
// +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中有效。

关于go - 使用gcc而不是g++构建golang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62636300/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com