I want, at build time, to define a string variable in cgo. None of the following approaches works.
#cgo CFLAGS: -DLOG="common"
'common' undeclared (first use in this function)
#cgo CFLAGS: -DLOG=common
'common' undeclared (first use in this function)
#cgo CFLAGS: -DLOG=\"common\"
malformed #cgo argument: -DLOG="common"
It appears this is not possible as cgo does some mangling/parsing -- can you get away with just a normal #define LOG "common"
(i.e. not use the CGO special flags).
Or failing that You can invoke go run
/go build
like this: CGO_CFLAGS='-DLOG="common"' go run so.go
you could define a variable in cgo same as define it in c code,
example:
package main
/*
int initflag=2;
int GetInitFlag(){
return initflag;
}
*/
import "C"
import "fmt"
// CFlag get c flag
func CFlag() int {
value := C.GetInitFlag()
return int(value)
}
func main() {
fmt.Println(CFlag())
}
string type must convert char* in c to string
// C string to Go string
func C.GoString(*C.char) string