英文:
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 <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!
答案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 <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
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论