Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git2go on Windows #155

Open
vadimi opened this issue Dec 15, 2014 · 12 comments
Open

git2go on Windows #155

vadimi opened this issue Dec 15, 2014 · 12 comments

Comments

@vadimi
Copy link

vadimi commented Dec 15, 2014

I'm trying to use git2go on Windows 7 x64. I was able to build libgit2 (through mingw) and install git2go package to usual pkg location (pkg\windows_amd64\github.com\libgit2\git2go.a). But when I try to import github.com/libgit2/git2go package and build an app I get the following errors:

github.com/libgit2/git2go(.text): git_blame_file: not defined
github.com/libgit2/git2go(.text): git_blame_free: not defined
github.com/libgit2/git2go(.text): git_blame_get_hunk_byindex: not defined
github.com/libgit2/git2go(.text): git_blame_get_hunk_byline: not defined
github.com/libgit2/git2go(.text): git_blame_get_hunk_count: not defined
github.com/libgit2/git2go(.text): git_blame_init_options: not defined
github.com/libgit2/git2go(.text): git_blob_create_frombuffer: not defined
github.com/libgit2/git2go(.text): git_blob_rawcontent: not defined
github.com/libgit2/git2go(.text): git_blob_rawsize: not defined
github.com/libgit2/git2go(.text): git_branch_create: not defined
github.com/libgit2/git2go(.text): git_branch_delete: not defined
github.com/libgit2/git2go(.text): git_branch_is_head: not defined
github.com/libgit2/git2go(.text): git_branch_iterator_free: not defined
github.com/libgit2/git2go(.text): git_branch_iterator_new: not defined
github.com/libgit2/git2go(.text): git_branch_lookup: not defined
github.com/libgit2/git2go(.text): git_branch_move: not defined
github.com/libgit2/git2go(.text): git_branch_name: not defined
github.com/libgit2/git2go(.text): git_branch_next: not defined
github.com/libgit2/git2go(.text): git_branch_remote_name: not defined
github.com/libgit2/git2go(.text): git_branch_set_upstream: not defined
github.com/libgit2/git2go(.text): git_branch_upstream: not defined

Any ideas on how to make it work on Windows?

@carlosmn
Copy link
Member

That looks like what happens when you don't use 'make install' to install the package. None of this should have anything to do with Windows, especially if you're building on MinGW which takes away the Windows system.

@carlosmn
Copy link
Member

If you did install via 'make install', then it looks like whatever compiler you're using does not accept the flags we pass in order to combine the static archive with libgit2 in it and script/with-static.sh would have to be modifed to detect the compiler and figure out how to pass data to it.

@vadimi
Copy link
Author

vadimi commented Dec 15, 2014

I tried to do it through make install as well. It works only when add -lws2_32 to CGO_LDFLAGS in script/with-static.sh and add -G"MinGW Makefiles" option to cmake in build-libgit2-static.sh (otherwise it uses Visual Studio). It installs successfully, but I get different errors while compiling target app:

github.com/libgit2/git2go(.text): strdup: not defined
github.com/libgit2/git2go(.text): strdup: not defined
github.com/libgit2/git2go(.text): strdup: not defined
github.com/libgit2/git2go(.text): close: not defined
github.com/libgit2/git2go(.text): close: not defined
github.com/libgit2/git2go(.text): close: not defined
github.com/libgit2/git2go(.text): strdup: not defined
github.com/libgit2/git2go(.text): strdup: not defined

@kjellski
Copy link

kjellski commented May 1, 2015

I've got a similar problem trying to build this on windows failed with go get, for both versions:

import "gopkg.in/libgit2/git2go.v22"

import "github.com/libgit2/git2go"

I'm always having the problem that gcc is told to look at the wrong directory:

# gopkg.in/libgit2/git2go.v22
gcc: error: Files: No such file or directory
gcc: error: (x86)/libgit2/include: No such file or directory

#github.com/libgit2/git2go
gcc: error: Files: No such file or directory
gcc: error: (x86)/libgit2/include: No such file or directory

Has anyone an idea on how to circumvent this? I'm completely new to go, any help would be greatly appreciated.

@clns
Copy link
Contributor

clns commented Jul 6, 2015

UPDATE 2

If specifying -lws2_32 to CGO_LDFLAGS as @vadimi said, the errors below disappear, instead these errors pop up:

...
C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.a(winhttp.c.obj):winhttp.c:(.text+0x212a): undefined reference to `WinHttpQueryOption'
C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.a(winhttp.c.obj):winhttp.c:(.text+0x217f): undefined reference to `__imp_CertFreeCertificateContext'
C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.a(winhttp.c.obj):winhttp.c:(.text+0x21fd): undefined reference to `WinHttpSetOption'
C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.a(winhttp.c.obj):winhttp.c:(.text+0x222f): undefined reference to `WinHttpSendRequest'
collect2.exe: error: ld returned 1 exit status

UPDATE 1

The compiled library git.lib was for x86, even though I was using an x64 cross compile command prompt. I managed to create an x64 version by specifying the generator to the cmake command, as -G"Visual Studio 12 Win64".

After more research I think I should use -G"MinGW Makefiles", which results in the following errors when running ./script/with-static.sh go install:

C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.a(index.c.obj):index.c:(.text+0xd04): undefined reference to `__imp_ntohl'
C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.a(index.c.obj):index.c:(.text+0xec1): undefined reference to `__imp_ntohs'
...
C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.a(winhttp.c.obj):winhttp.c:(.text+0x19f4): undefined reference to `__imp_CertFreeCertificateContext'
...
C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.a(socket_stream.c.obj):socket_stream.c:(.text+0x44): undefined reference to `__imp_closesocket'
C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.a(socket_stream.c.obj):socket_stream.c:(.text+0x4f): undefined reference to `__imp_WSACleanup'
...
C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.a(socket_stream.c.obj):socket_stream.c:(.text+0x3a6): undefined reference to `__imp_WSAGetLastError'
collect2.exe: error: ld returned 1 exit status

ORIGINAL MESSAGE

Is anyone able to make this work on Windows?

I managed to compile libgit2, have it installed at C:\libgit2 (to avoid spaces in the path, like the error @kjellski mentioned), set the PKG_CONFIG_PATH to C:\libgit2\lib\pkgconfig, but when I run go install it says ld.exe skipping incompatible git2.lib, like below:

$ go install
# github.com/libgit2/git2go
C:/mingw-w64/x86_64-5.1.0-win32-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mi
ngw32/bin/ld.exe: skipping incompatible .\vendor\libgit2\build\Debug/git2.lib when searching for -lgit2
C:/mingw-w64/x86_64-5.1.0-win32-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mi
ngw32/bin/ld.exe: skipping incompatible .\vendor\libgit2\build\Debug/git2.lib when searching for -lgit2
C:/mingw-w64/x86_64-5.1.0-win32-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mi
ngw32/bin/ld.exe: skipping incompatible .\vendor\libgit2\build\Debug\git2.lib when searching for -lgit2
C:/mingw-w64/x86_64-5.1.0-win32-seh-rt_v4-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mi
ngw32/bin/ld.exe: cannot find -lgit2
collect2.exe: error: ld returned 1 exit status

I'm using:

The complete list of commands is:

go get -d github.com/libgit2/git2go
cd $GOPATH/src/github.com/libgit2/git2go
git checkout next
git submodule update --init # get libgit2
cd vendor/libgit2

mkdir build
cd build
cmake -DTHREADSAFE=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=C:/libgit2 ..
cmake --build . --target install

cd ..\..\..
$env:PKG_CONFIG_PATH="C:\libgit2\lib\pkgconfig"
go install
# errors follow ...

I also tried the make install method using MSYS2's MINGW64, that also fails with output such as:

[100%] Linking C static library libgit2.a
mingw32-make.exe[3]: Leaving directory 'C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build'
[100%] Built target git2
mingw32-make.exe[2]: Leaving directory 'C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build'
mingw32-make.exe[1]: Leaving directory 'C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build'
./script/with-static.sh go install ./...
+ export BUILD=C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build
+ BUILD=C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build
+ export PCFILE=C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.pc
+ PCFILE=C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.pc
++ pkg-config --static --libs C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.pc
+ FLAGS='-LC:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/install/lib -lgit2 '
+ export 'CGO_LDFLAGS=C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.a -LC:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build -LC:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/install/lib -lgit2  -lws2_32'
+ CGO_LDFLAGS='C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.a -LC:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build -LC:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/install/lib -lgit2  -lws2_32'
+ export CGO_CFLAGS=-IC:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/include
+ CGO_CFLAGS=-IC:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/include
+ go install ./...
# github.com/libgit2/git2go
C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.a(winhttp.c.obj): In function `winhttp_stream_close':
C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/src/transports/winhttp.c:261: undefined reference to `WinHttpCloseHandle'
C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.a(winhttp.c.obj): In function `write_chunk':
C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/src/transports/winhttp.c:498: undefined reference to `WinHttpWriteData'
...
C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/build/libgit2.a(winhttp.c.obj): In function `put_uuid_string':
C:/Users/Calin/go/src/github.com/libgit2/git2go/vendor/libgit2/src/transports/winhttp.c:995: undefined reference to `__imp_UuidCreate'
collect2.exe: error: ld returned 1 exit status
makefile:11: recipe for target 'install' failed
mingw32-make: *** [install] Error 2

@vadimi
Copy link
Author

vadimi commented Jul 6, 2015

@clns I tried the same things and couldn't get it working. So I ended up compiling libgit2 to a separate dll and used dynamic linking instead of static one. I believe that there are some bugs in cgo (see golang/go#7555) that will be fixed in go 1.5, so I'd suggest to try once again then.

@clns
Copy link
Contributor

clns commented Jul 6, 2015

Thanks, I ended up compiling it using -G"MinGW Makefiles" into a .dll and it did install. However using git2go in a program is still not working, it crashes pretty bad 😄

image

It could be that I overlooked something because I tried so many combinations today that I'm overwhelmed right now.

@vadimi
Copy link
Author

vadimi commented Jul 6, 2015

Try to put libgit2.dll side by side with your executable or somewhere in your %PATH%.

@clns
Copy link
Contributor

clns commented Jul 6, 2015

That worked, thanks!!! Should be interesting to see if this can be fixed before Go 1.5. Till' then, I guess this solution would work.

@clns
Copy link
Contributor

clns commented Aug 31, 2015

Anyone having any luck compiling git2go as a static library on Windows amd64 with go 1.5? I'm trying the steps from above but with the same results.

$ go install
C:/libgit2/lib/libgit2.a(index.c.obj):index.c:(.text+0xd04): undefined reference to `__imp_ntohl'
C:/libgit2/lib/libgit2.a(index.c.obj):index.c:(.text+0x59ab): undefined reference to `__imp_htonl'
...
C:/libgit2/lib/libgit2.a(winhttp.c.obj):winhttp.c:(.text+0x68): undefined reference to `WinHttpWriteData'
...
C:/libgit2/lib/libgit2.a(indexer.c.obj):indexer.c:(.text+0x853): undefined reference to `__imp_ntohl'
...

I have compiled libgit2 without any errors, at vendor\libgit2\build\libgit2.a.

@carlosmn
Copy link
Member

As far as the WinHTTP errors, adding -lwinhttp -lcrypt32 -lrpcrt4 -lole32 to CGO_LDFLAGS in ./script/with-static.sh should make it work (those don't get written to the .pc file atm as they don't need to get detected).

If it's completely failing to find the libc/crt functions... then I don't know. The Windows system where I have go installed fails to build libgit2 with gcc so I haven't been able to investigate more.

@clns
Copy link
Contributor

clns commented Apr 30, 2016

I was able to successfully build on Windows from a MinGW Shell installed using MSYS2. See PR #307

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants