Search code examples
gocgo

Error: Could not determine kind of name for C.stdout when building example from C? Go? Cgo! article


I'm trying to build the following example from C? Go? Cgo!:

package print

/*
#include <stdio.h>
#include <stdlib.h>
*/
import "C"
import "unsafe"

func Print(s string) {
    cs := C.CString(s)
    C.fputs(cs, (*C.FILE)(C.stdout))
    C.free(unsafe.Pointer(cs))
}

I'm running Go on Win7 64 and am using the 64 bit version of GCC from http://tdm-gcc.tdragon.net/ Running this on Linux isn't an option.

The error I get is:

could not determine kind of name for C.stdout

I haven't been able to find any documentation on this message, and very few hits show up on Google.

Does anyone have ideas on what's causing this? Thanks in advance!


Solution

  • Here is one way to access C.stdout on windows:

    // Copyright 2009 The Go Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style
    // license that can be found in the LICENSE file.
    
    package stdio
    
    /*
    #include <stdio.h>
    // on mingw, stderr and stdout are defined as &_iob[FILENO]
    // on netbsd, they are defined as &__sF[FILENO]
    // and cgo doesn't recognize them, so write a function to get them,
    // instead of depending on internals of libc implementation.
    FILE *getStdout(void) { return stdout; }
    FILE *getStderr(void) { return stderr; }
    */
    import "C"
    
    var Stdout = (*File)(C.getStdout())
    var Stderr = (*File)(C.getStderr())
    

    https://github.com/golang/go/blob/master/misc/cgo/stdio/stdio.go