如何从 Windows 上的本机 C++ 应用程序使用 libgit2?(微软VC++) [英] How to use libgit2 from a native C++ application on Windows? (Microsoft VC++)
问题描述
总结: 有没有关于如何构建使用 libgit2 库(可在 GitHub 获得资源)?
Summary: Is there any tiny example on how to build the Win32 C++ console application that uses the libgit2 library (sources available at GitHub)?
我遵循了关于如何构建库的自述文件和维基在 Windows 上使用 CMake,并且运行顺利(只有一个单元测试错误).
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).
现在,我想构建一个使用该库的简单控制台应用程序——使用 Microsoft Visual Studio C++ 2010(the.vcxproj
和 the.sln
).我在 libgit2 示例中找到了 general.c
代码,我想从我的 C++ 应用程序中做同样的事情.没有成功——我今天感觉很愚蠢...
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...
我注意到有用于 C++ 和 Qt 的 libqgit2.纯 C++ 有没有类似的东西?或者有没有办法从 C++ 应用程序中使用 C 库?
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?
更新:我已将 general.c
重命名为 general.cpp
,添加了项目的 libgit2 包含路径和.lib 文件,将 #include
更改为 #include
,并编译.出现以下链接错误:
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 ==========
推荐答案
这强烈暗示了调用约定不匹配 给我.出于多种原因,libgit2 默认使用 __stdcall
,而 Visual Studio 默认创建使用 __cdecl
调用约定的项目.虽然您的程序可以使用任一调用约定并使用不同的调用约定成功调用 libgit2,但最简单的解决方案可能只是对两者使用相同的调用约定.
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.
当您配置 libgit2 时,您可以关闭 STDCALL
标志,这将导致它发出一个使用 __cdecl
调用约定构建的库:
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 .
这篇关于如何从 Windows 上的本机 C++ 应用程序使用 libgit2?(微软VC++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!