dooso0594 2012-09-25 22:05
浏览 68
已采纳

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

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!

  • 写回答

2条回答 默认 最新

  • dsajkdadsa14222 2012-09-26 05:47
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?