Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: (func literal)() used as value #47971

Closed
ShinnZhang opened this issue Aug 26, 2021 · 1 comment
Closed

cmd/cgo: (func literal)() used as value #47971

ShinnZhang opened this issue Aug 26, 2021 · 1 comment

Comments

@ShinnZhang
Copy link

ShinnZhang commented Aug 26, 2021

https://golang.hotexamples.com/site/file?hash=0xf212ebbab7d048e644b3240aa36ecf680880023138965f893980a2439aada191&fullName=stage1/init.go&project=kevin-zhaoshuai/rkt

I found this and try to copy it.

code in go file

/*
#cgo CFLAGS: -ImyCPath
#cgo LDFLAS: -ldl
#include <dlfcn.h>
#include "myHeaderFile.h"
MyType my_type_creater(void *f, const char* s)
{
    MyType (*MyFunc)(const char*);
    MyFunc = (MyType (*)(const char*))f;
    return MyFunc(s);
}
*/
import "C"
import (
    "unsafe"
    "fmt"
)

func getCFunc() (unit string, err error) {
	libpath := C.CString(libraryPath)
	defer C.free(unsafe.Pointer(libpath))
	handle := C.dlopen(libpath, C.RTLD_LAZY)
	if handle == nil {
		err = fmt.Errorf("error opening library.so")
		return
	}
	defer func() {
		if r := C.dlclose(handle); r != 0 {
			err = fmt.Errorf("error closing library.so")
		}
	}()

	sym := C.CString("MyFunc")
	defer C.free(unsafe.Pointer(sym))

	MyFunc := C.dlsym(handle, sym)
	if C_Trainer_Create == nil {
		err = fmt.Errorf("error resolving MyFunc")
		return
	}
       
	s:= C.CString("")
	defer C.free(unsafe.Pointer(s))
	ret:= C.my_trainer_create(MyFunc, s) // this line is 54
	fmt.Println(ret)
	return
}

Output:

# command-line-arguments
./testTrainer.go:54:133: (func literal)() used as value

my go version:

$ go version
go version go1.16.7 linux/amd64
@ShinnZhang ShinnZhang changed the title cmd/cgo: (func literal)() used as value after dlopen() c++ library cmd/cgo: (func literal)() used as value Aug 26, 2021
@seankhliao
Copy link
Member

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@golang golang locked and limited conversation to collaborators Aug 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants