dopuz8728 2013-08-14 18:07
浏览 43
已采纳

未指定cgo SDL函数

I've tried looking this up everywhere and tried just about everything I can, I cannot get SDL to work in Go. Here's my code, a little sloppy, but I just wanted to get it to work, just to test it, just as a starting point. I was going to clean it up and get rid of all the unnecessary #defines later.

package main

// #cgo LDFLAGS: -lSDL -lSDL_main -lSDL_image
// #include <stdio.h>
// #include <SDL/SDL.h>
// #include <SDL/SDL_main.h>
// #include <SDL/SDL_image.h>
import "C"

import "runtime"

func init() {
runtime.LockOSThread()
}

func main() {
var image* C.SDL_Surface
var screen* C.SDL_Surface
C.SDL_Init( C.SDL_INIT_EVERYTHING )
screen = C.SDL_SetVideoMode(640, 480, 32, C.SDL_SWSURFACE)
hello = C.SDL_LoadBMP( "moe.bmp" )
C.SDL_BlitSurface(hello, nil, screen, nil)
C.SDL_Flip(screen)
C.SDL_Delay(2000)
C.SDL_FreeSurface(hello)
C.SDL_Quit()
}

This gives me the "command-line arguments" error: 'SDL_LoadBMP' undeclared (first use in this function), and no amount of searching or wizardry will fix it. If it's required, I'm on Ubuntu with SDL 1.0.2 (I think)

I'd rather not use a wrapper since the only SDL 1 wrapper for Go is two years old and the two good ones are for SDL 2, and I'd rather use SDL 1. Thank you anyone that can guide me.

Also, bonus question because the compiler would never let me test it (obviously), do I use "nil", "NULL", or "C.NULL" (which I know I'd have to #define) in BlitSurface's arguments?

  • 写回答

3条回答 默认 最新

  • douchun3680 2013-08-14 18:36
    关注

    SDL_LoadBMP is a macro, not a function. Its definition is

    #define SDL_LoadBMP(file)   SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)
    

    Cgo won't be able to use macros however, so you have two options: Either use the expanded version of the macro or, which would be preferred, write a C function that uses the macro and call that function from Go.

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

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码