0

Summary: Is there any tiny example on how to build the Win32 C++ console application that uses the libgit2 library (sources available at GitHub)?

I have followed the readme and the wiki on how to build the library on Windows using the CMake, and it worked smoothly (only a single unit-test error).

Now, I want to build a simple console application that uses the library -- using Microsoft Visual Studio C++ 2010 (the.vcxproj and the.sln). I have found the general.c code in the libgit2 examples, and I would like to do the same from my C++ application. No success -- I feel really stupid today...

I have noticed there is libqgit2 for C++ and Qt. Is there anything similar for pure C++? Or is there a way to use the C library from C++ application?

Update: I have renamed the general.c to general.cpp, added the libgit2 include path to the project and the path to the .lib files, changed the #include <stdio.h> to #include <cstdio>, and compiled. The following link errors appear:

1>------ Build started: Project: libgit2_general, Configuration: Debug Win32 ------
1>general.obj : error LNK2019: unresolved external symbol _git_repository_free referenced in function _main
1>general.obj : error LNK2019: unresolved external symbol _git_config_get_string referenced in function _main
1>general.obj : error LNK2019: unresolved external symbol _git_config_get_int32 referenced in function _main
1>general.obj : error LNK2019: unresolved external symbol _git_config_open_ondisk referenced in function _main
1>general.obj : error LNK2019: unresolved external symbol _git_strarray_free referenced in function _main
... etc.
1>general.obj : error LNK2019: unresolved external symbol _git_repository_open referenced in function _main
1>general.obj : error LNK2019: unresolved external symbol _giterr_last referenced in function "void __cdecl check_error(int,char const *)" (?check_error@@YAXHPBD@Z)
1>D:\Tutorial\libgit2_general\Debug\libgit2_general.exe : fatal error LNK1120: 56 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
pepr
  • 20,112
  • 15
  • 76
  • 139
  • 1
    What do you mean when you say "No success"? What kind of problems are you having? (Note, if you're having linking errors, that you must ensure that you're using compatible calling conventions: libgit2 uses `__stdcall` by default and MSVC creates `__cdecl` projects by default!) – Edward Thomson Jun 05 '13 at 14:00

2 Answers2

3

This strongly suggests a calling convention mismatch to me. libgit2 uses __stdcall by default, for a number of reasons, while Visual Studio defaults to creating projects that use the __cdecl calling convention. While your program can use either calling convention and successfully call libgit2 using a different one, the easiest solution is probably just to use the same calling convention for both.

When you configure libgit2, you can turn off the STDCALL flag, which will cause it to emit a library built with __cdecl calling conventions:

cmake %PATH_TO_LIBGIT2_SOURCE% -DTHREADSAFE=ON -DSTDCALL=OFF
cmake --build .
Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
  • It seems it was exactly the reason. It compiles fine now. Running the example caused an assert, but this is another story. Thanks a lot ;) – pepr Jun 06 '13 at 07:37
  • very helpful indeed! Any ideas why MSVC uses SysV ABI by default? :) – rostamn739 Jun 09 '16 at 21:20
  • 1
    @rostamn739 This q&a is now a bit outdated, libgit2 now uses `cdecl` by default to match the rest of the defaults (msvc, win32 api, etc). Now if one wants `stdcall` (presumably for .NET ffi) then one can specify `-DSTDCALL=ON` during the cmake phase. – Edward Thomson Jun 10 '16 at 00:26
1

It's a little surprising if you generated the project from CMake, bit it could be that you're not linking to libgit2.lib. Make sure you have git2.dll in Project Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies.

link to git2.lib

Ben Straub
  • 5,675
  • 3
  • 28
  • 43
  • I believe I did it that way. The only difference I can see is that I put the `Debug` directory to the *Aditional Library Directories* and then used the plain `git2.lib` in the list of *Aditional Dependencies*. The only other thing that just came to my mind is what Runtime Library must be set via the C/C++ Code Generation. – pepr Jun 06 '13 at 07:29
  • 1
    Just being curious... Does that *flat window-look* come from Visual Studio 2012, or is it because you use Windows 8? (Frankly, I feel sometimes as if Microsoft took us as test animals.) – pepr Jun 06 '13 at 07:33
  • Do you compile libgit2 via the VC++ 2012, or via VC++ 2010? – pepr Jun 07 '13 at 08:35
  • 1
    Both, on different days. :) CMake can generate project files for either by using `-G "Visual Studio 10"` or `-G "Visual Studio 11"`. – Ben Straub Jun 07 '13 at 15:12