0

I want, at build time, to define a string variable in cgo. None of the following approaches works.

  1. #cgo CFLAGS: -DLOG="common"  
    

    'common' undeclared (first use in this function)

  2. #cgo CFLAGS: -DLOG=common
    

    'common' undeclared (first use in this function)

  3. #cgo CFLAGS: -DLOG=\"common\"
    

    malformed #cgo argument: -DLOG="common"

user2424276
  • 591
  • 1
  • 5
  • 24

2 Answers2

0

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

Ash Berlin-Taylor
  • 3,879
  • 29
  • 34
0

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

https://golang.org/cmd/cgo/