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

StackOverflow https://stackoverflow.com/questions/12591773

  •  03-07-2021
  •  | 
  •  

Domanda

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!

È stato utile?

Soluzione

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

Altri suggerimenti

In the cgo implementation there is a method to guess the types according to the output of gcc. It could be possible that you have set a different locale in your terminal and the guessing fails.

Try this:

LC_ALL=C go build
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top