无法导入 go-gl 库 - “构建约束排除所有 Go 文件”

问题描述 投票:0回答:1

我对 golang 很陌生,如果这是显而易见的,我很抱歉。我已经按照项目Github中的步骤进行操作,这是我的确切过程(有错误):

$ go mod init mydomain.com/test
$ go mod tidy
$ go get -u github.com/go-gl/gl/v4.6-core/gl
$ go run .
package evlis.org/nemo
        imports github.com/go-gl/gl: build constraints exclude all Go files in C:\Users\myname\go\pkg\mod\github.com\go-gl\[email protected]

为什么找不到

go-gl
包??

这是 go.mod 文件:

module evlis.org/nemo

go 1.22.4

require github.com/go-gl/glfw/v3.3/glfw v0.0.0-20240506104042-037f3cc74f2a
require github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71

这是我的完整 main.go:

package main

import (
    "log"
    "runtime"

    "github.com/go-gl/gl/v4.6-core/gl"
    "github.com/go-gl/glfw/v3.3/glfw"
)

const (
    width  = 500
    height = 500
)

func main() {
    runtime.LockOSThread()

    window := initGlfw()
    defer glfw.Terminate()

    program := initOpenGL()

    for !window.ShouldClose() {
        draw(window, program)
    }
}

// initGlfw initializes glfw and returns a Window to use.
func initGlfw() *glfw.Window {
    if err := glfw.Init(); err != nil {
        panic(err)
    }

    glfw.WindowHint(glfw.Resizable, glfw.False)
    glfw.WindowHint(glfw.ContextVersionMajor, 4) // OR 2
    glfw.WindowHint(glfw.ContextVersionMinor, 1)
    glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
    glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)

    window, err := glfw.CreateWindow(width, height, "Conway's Game of Life", nil, nil)
    if err != nil {
        panic(err)
    }
    window.MakeContextCurrent()

    return window
}

// initOpenGL initializes OpenGL and returns an intiialized program.
func initOpenGL() uint32 {
    if err := gl.Init(); err != nil {
        panic(err)
    }
    version := gl.GoStr(gl.GetString(gl.VERSION))
    log.Println("OpenGL version", version)

    prog := gl.CreateProgram()
    gl.LinkProgram(prog)
    return prog
}

func draw(window *glfw.Window, program uint32) {
    gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
    gl.UseProgram(program)

    glfw.PollEvents()
    window.SwapBuffers()
}

我在网上搜索了类似问题的特定存储库,我发现的最接近的是this,但是

go clean -modcache
对我的情况没有帮助......

go opengl import
1个回答
1
投票

非常感谢@Brits为我指明了正确的方向。

问题出在我的系统上没有

gcc
编译器。为了解决这个问题,我下载了 GCC 14.1.0 Win64 - 没有 LLVM/Clang/LLD/LLDB,将提取的
mingw64\bin
添加到我的 用户路径环境变量,然后重新运行以下
go
命令:

$ go env -w "CGO_ENABLED=1"
$ go run .

成功。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.