错误:在构建来自C?Go?Cgo!文章的示例时,无法确定C.stdout的名称类型。

huangapple go评论76阅读模式
英文:

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

问题

我正在尝试从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上几乎没有任何结果。

有人知道是什么原因引起的吗?提前谢谢!

英文:

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

package print

/*
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
*/
import &quot;C&quot;
import &quot;unsafe&quot;

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!

答案1

得分: 4

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

// 版权所有 2009 年 Go 作者。保留所有权利。
// 使用此源代码受 BSD 风格的许可证管辖
// 可在 LICENSE 文件中找到许可证。

package stdio

/*
#include <stdio.h>
// 在 mingw 上,stderr 和 stdout 被定义为 &_iob[FILENO]
// 在 netbsd 上,它们被定义为 &__sF[FILENO]
// 而 cgo 不识别它们,所以编写一个函数来获取它们,
// 而不是依赖于 libc 实现的内部。
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

英文:

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 &lt;stdio.h&gt;
// on mingw, stderr and stdout are defined as &amp;_iob[FILENO]
// on netbsd, they are defined as &amp;__sF[FILENO]
// and cgo doesn&#39;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 &quot;C&quot;

var Stdout = (*File)(C.getStdout())
var Stderr = (*File)(C.getStderr())

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

答案2

得分: 0

在cgo实现中,有一个方法根据gcc的输出来猜测类型。可能是因为您在终端中设置了不同的区域设置,导致猜测失败。

请尝试以下命令:

LC_ALL=C go build
英文:

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

huangapple
  • 本文由 发表于 2012年9月26日 06:05:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/12591773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定