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条)

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器