错误:从C构建示例时无法确定C.stdout的名称类型?走? CGO!文章

时间:2012-09-25 22:05:40

标签: go cgo

我正在尝试从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))
}

我在Win7 64上运行Go并使用http://tdm-gcc.tdragon.net/中的64位GCC版本 在Linux上运行它不是一种选择。

我得到的错误是:

could not determine kind of name for C.stdout

我无法找到有关此邮件的任何文档,而且很少有点击显示在Google上。

有没有人对导致这种情况的原因有所了解?提前谢谢!

2 个答案:

答案 0 :(得分:4)

以下是在Windows上访问C.stdout的一种方法:

// 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

答案 1 :(得分:0)

在cgo实现中,根据gcc的输出有一个method来猜测类型。您可能已在终端中设置了不同的区域设置,并且猜测失败。

试试这个:

LC_ALL=C go build