help working with cgo on windows

2,703 views
Skip to first unread message

qeed qeed

unread,
May 6, 2012, 1:48:05 PM5/6/12
to golang-nuts
Hi, I am trying to get SDL and GLEW to work on the windows port. I am
using windows 64 bit using the 6g compilers. I used mingw64 and msys
that I got from

http://sourceforge.net/projects/mingw-w64/files/latest/download?source=files

I compiled SDL from source for mingw and installed it to /usr/local/.
For GLEW i just copied the binary distribution into the top mingw
folder. I can compile a C SDL/GLEW application from mingw using

cc -o app app.c `sdl-config --cflags --libs` -lglew -I/usr/include -
lopengl32

and it seems to work.

but trying cgo on windows with this code

package main

// #cgo windows CFLAGS: -I/usr/local/include/SDL -D_GNU_SOURCE=1
// #cgo windows LDFLAGS: -lglew32 -lopengl32
// #include <SDL.h>
import "C"

func main() {
}

and i got SDL.h no such file or directory when i try to build. I am
not sure what flags I should pass to the CFLAGS, other than the
include which i gotten from sdl-config. I tried changed the /usr/
directory to /c/ming64/local/include and C:\ming64\local\include\ in
case it used that naming convention

Not sure how to get that to work. Any hints is appreciated, thanks.

Jared Anderson

unread,
May 7, 2012, 12:16:24 AM5/7/12
to golan...@googlegroups.com
You need to use a windows path.  The link you gave does not include
msys as far as I can tell so I'm not sure how you set that up.  Based
on the information you gave I am guessing you want something like
C:\ming64\msys\1.0\local\include\SDL.

minux

unread,
May 7, 2012, 8:09:07 AM5/7/12
to qeed qeed, golang-nuts
On Mon, May 7, 2012 at 1:48 AM, qeed qeed <qeed...@gmail.com> wrote:
cc -o app app.c `sdl-config --cflags --libs` -lglew -I/usr/include -
lopengl32

and it seems to work.

but trying cgo on windows with this code

package main

// #cgo windows CFLAGS: -I/usr/local/include/SDL -D_GNU_SOURCE=1
#cgo CFLAGS: .... 
// #cgo windows LDFLAGS: -lglew32 -lopengl32
ditto 

qeed qeed

unread,
May 7, 2012, 9:10:04 AM5/7/12
to golang-nuts
i have changed the flags and this is my test case now


/*
package main

/*
#cgo CFLAGS: -IC:\mingw64\local\include
#cgo LDFLAGS: -LC:\mingw64\local\lib -lmingw32 -lSDL
#include <SDL.h>
*/
import "C"

func main() {
C.SDL_Init(C.SDL_INIT_EVERYTHING)
}

and it still cant find the SDL header.

To be specific my mingw folder is

C:\mingw64 and all of msys is in the top level ie, C:\mingw64\bin will
have bash.exe etc. I wanted to make this specific because mingw64 is
separate from msys and you have to install that manually.
my sdl headers are in C:\mingw64\local\include\SDL
and the sdl libs are in C:\mingw64\local\lib
is there anyway to see more error message from cgo as in what path is
it doing or if it can find the paths?

qeed qeed

unread,
May 7, 2012, 9:24:24 AM5/7/12
to golang-nuts
hmm this works now
package main

/*
#cgo CFLAGS: -IC:/mingw64/local/include/SDL -IC:/mingw64/include
#cgo LDFLAGS: -LC:/mingw64/local/lib -lmingw32 -lSDLmain -lSDL -
lglew32 -lopengl32
#include <SDL.h>
#include <GL/glew.h>
*/
import "C"
import "runtime"

func main() {
runtime.LockOSThread()
C.SDL_Init(C.SDL_INIT_EVERYTHING)
C.SDL_SetVideoMode(800, 600, 32, C.SDL_OPENGL)
C.glewInit()
for {
C.glClear(C.GL_COLOR_BUFFER_BIT)
C.glClearColor(.4,.4,.4,.4)
C.SDL_GL_SwapBuffers()
}
}

thanks for the help.
Reply all
Reply to author
Forward
0 new messages