MSYS2 and cgo

901 views
Skip to first unread message

Jacob Marble

unread,
Nov 25, 2015, 4:44:24 PM11/25/15
to golang-nuts
The environment is MSYS2 on Windows 8.1, and I mostly program for Unix, so this is new for me. Go 1.4.2 and other packages installed with pacman. The problem described also occurs with Go 1.5.1, downloaded from golang.org.

My goal: use Poppler from Go, in a Windows build. A Go package for Poppler exists on GitHub, but it fails to build just like this little test. The problem is that gcc can't find the Poppler header files, located in /mingw64/include/poppler

The following C program builds fine:
#include <glib/poppler.h>
int main() {
  return 0;
}

...with this command:
gcc poppler.c -o p `pkg-config --cflags poppler-glib`

The following Go program fails to build:

package main


/*

#cgo pkg-config: --cflags poppler-glib

#include <glib/poppler.h>

*/

import "C"

import "fmt"


func main() {

        var p *C.PopplerDocument

        fmt.Println(p)

}


...with this command:

$ go install

.\poppler.go:5:26: fatal error: glib/poppler.h: No such file or directory

compilation terminated.


Output of pkg-config --cflags poppler-glib:

-pthread -mms-bitfields -I/mingw64/include/poppler/glib -I/mingw64/include/poppler -I/mingw64/include/cairo -I/mingw64/include/pixman-1 -I/mingw64/include -I/mingw64/include/freetype2 -I/mingw64/include/libpng16 -I/mingw64/include/harfbuzz -I/mingw64/include/glib-2.0 -I/mingw64/lib/glib-2.0/include -I/mingw64/include -I/mingw64/include/freetype2 -I/mingw64/include -I/mingw64/include/harfbuzz -I/mingw64/include/glib-2.0 -I/mingw64/lib/glib-2.0/include -I/mingw64/include/libpng16


Replace [`pkg-config --cflags poppler-glib`] with [-I/mingw64/include/poppler] in C; build fails:

$ gcc poppler.c -o p -I/mingw64/include/poppler

In file included from poppler.c:1:0:

C:/msys64/mingw64/include/poppler/glib/poppler.h:22:25: fatal error: glib-object.h: No such file or directory

compilation terminated.


So it found glib/poppler.h and now it needs glib-object.h. That's fine. Replace [#cgo pkg-config: --cflags poppler-glib] with [#cgo CFLAGS: -I/mingw64/include/poppler] in Go; build fails:

$ go install

.\poppler.go:5:26: fatal error: glib/poppler.h: No such file or directory

compilation terminated.


So the problem is likely not pkg-config, but how Go/cgo handles the path indicated in the -I flag. I've tried replacing [-I/mingw64/include/poppler] with [-Ic:\\msys64\\mingw64\\include\\poppler] but that isn't valid.


Any clues as to why this is happening?

How do you setup a Go dev environment in MSYS2?

Ian Lance Taylor

unread,
Nov 25, 2015, 5:13:56 PM11/25/15
to Jacob Marble, golang-nuts
On Wed, Nov 25, 2015 at 1:44 PM, Jacob Marble <jacob...@google.com> wrote:
>
> So the problem is likely not pkg-config, but how Go/cgo handles the path
> indicated in the -I flag. I've tried replacing [-I/mingw64/include/poppler]
> with [-Ic:\\msys64\\mingw64\\include\\poppler] but that isn't valid.

I don't know what is happening, but all Go does with those options is
pass them to the compiler. Run `go install -x` to see how the options
are passed to the cgo program. Run the cgo program with -debug-gcc to
see how it invokes the compiler.

Ian

Jacob Marble

unread,
Nov 25, 2015, 5:40:53 PM11/25/15
to Ian Lance Taylor, golang-nuts
Thanks Ian, I had not looked to [go install -x] for clues.

$ go install -x
WORK=C:\msys64\tmp\go-build890903279
mkdir -p $WORK\github.com\path\p\_obj\
mkdir -p $WORK\github.com\path\p\_obj\exe\
cd C:\msys64\home\jacobmarble\go\src\github.com\path\p
CGO_LDFLAGS="-g" "-O2" "C:\\msys64\\mingw64\\lib\\go\\pkg\\tool\\windows_amd64\\cgo.exe" -objdir "C:\\msys64\\tmp\\go-build890903279\\github.com\\path\\p\\_obj\\" -- -I "C:\\msys64\\tmp\\go-build890903279\\github.com\\path\\p\\_obj\\" -I/mingw64/include/poppler poppler.go
.\poppler.go:5:26: fatal error: glib/poppler.h: No such file or directory
compilation terminated. 

Looks like the C:\\msys64\\foo path format is intact there. Try the very same format in the #cgo line:

#cgo CFLAGS: -I "C:\\msys64\\mingw64\\include\\poppler\\"

$ go install -x -work
can't load package: package github.com/path/p: C:\msys64\home\jacobmarble\go\src\github.com\path\p\poppler.go: malformed #cgo argument: C:\msys64\mingw64\include\poppler\

OK, go through each step from [go install -x] above, but replace the -I path that I care about:

$ CGO_LDFLAGS="-g" "-O2" "C:\\msys64\\mingw64\\lib\\go\\pkg\\tool\\windows_amd64\\cgo.exe" -objdir "C:\\msys64\\tmp\\go-build774476339\\github.com\\google\\cups-connector\\gcp-windows-connector\\p\\_obj\\" -- -I "C:\\msys64\\tmp\\go-build774476339\\github.com\\google\\cups-connector\\gcp-windows-connector\\p\\_obj\\" -I "C:\\msys64\\mingw64\\include\\poppler" poppler.go
bash: -O2: command not found

Remove [CGO_LDFLAGS="-g" "-O2"] from that command:

$ "C:\\msys64\\mingw64\\lib\\go\\pkg\\tool\\windows_amd64\\cgo.exe" -objdir "C:\\msys64\\tmp\\go-build774476339\\github.com\\google\\cups-connector\\gcp-windows-connector\\p\\_obj\\" -- -I "C:\\msys64\\tmp\\go-build774476339\\github.com\\google\\cups-connector\\gcp-windows-connector\\p\\_obj\\" -I "C:\\msys64\\mingw64\\include\\poppler" poppler.go
In file included from C:\msys64\home\jacobmarble\go\src\github.com\google\cups-connector\gcp-windows-connector\p\poppler.go:6:0:
C:\msys64\mingw64\include\poppler/glib/poppler.h:22:25: fatal error: glib-object.h: No such file or directory
compilation terminated.

That's the error I want!

Any ideas how to fix this problem in a more general way?
I have a feeling that this has something to do with the difference between msys2 and mingw64, so I'm going to read some more now.

Jacob

Jacob Marble

unread,
Nov 25, 2015, 5:59:44 PM11/25/15
to Ian Lance Taylor, golang-nuts
On Wed, Nov 25, 2015 at 2:40 PM, Jacob Marble <jacob...@google.com> wrote:
Any ideas how to fix this problem in a more general way?
I have a feeling that this has something to do with the difference between msys2 and mingw64, so I'm going to read some more now.

After switching to the MinGW-w64 shell instead of the MSYS2 shell, things are coming together. I had to "patch" the MSYS2 environment with extra PATHs and such; unwinding that, and installing the MinGW-w64 build toolchain, seems to have enabled using [#cgo pkg-config].

Thanks for the -x clue, Ian.

Jacob 
Reply all
Reply to author
Forward
0 new messages