VS2022 MFC项目中vcpkg集成libgit2与libssh2报错求助
问题:libgit2执行git push报错"unsupported URL protocol"
在VS2022环境下的MFC项目中,通过vcpkg清单文件引入libgit2与libssh2依赖,调用libgit2执行git push操作时出现错误“unsupported URL protocol”。经排查推测问题根源是libgit2未关联libssh2,但不知道如何解决。
我的vcpkg清单文件
{ "name": "workbench", "version-string": "1.0.0.0", "dependencies": [ { "name": "openssl", "version>": "3.0.7#2" }, { "name": "libssh2", "version>": "1.10.0#4" }, { "name": "boost-beast", "version>": "1.78.0" }, { "name": "boost-asio", "version>": "1.78.0" }, { "name": "boost-process", "version>": "1.78.0" }, { "name": "boost-filesystem", "version>": "1.78.0" }, { "name": "boost-locale", "version>": "1.78.0" }, { "name": "boost-regex", "version>": "1.78.0" }, { "name": "boost-algorithm", "version>": "1.78.0" }, { "name": "boost-format", "version>": "1.78.0" }, { "name": "boost-multiprecision", "version>": "1.78.0" }, { "name": "boost-crc", "version>": "1.78.0" }, { "name": "boost-uuid", "version>": "1.78.0" }, { "name": "boost-iostreams", "version>": "1.78.0" }, { "name": "boost-interprocess", "version>": "1.78.0" }, { "name": "boost-date-time", "version>": "1.78.0" }, { "name": "zlib", "version>": "1.2.12#1" }, { "name": "bzip2", "version>": "1.0.8#2" }, { "name": "mongo-cxx-driver", "version>": "3.6.5" }, { "name": "libmariadb", "version>": "3.1.15" }, { "name": "hunspell", "version>": "1.7.1#2" }, { "name": "exprtk", "version>": "2022-01-01#2" }, { "name": "libxlsxwriter", "version>": "1.1.5" }, { "name": "libgit2", "version>": "1.4.2" } ], "builtin-baseline": "062f92c92e0e8017929ea7647819036d8daef638" }
我的调用代码
giterr_clear(); bool result = false; if (m_repo_path.empty()) return result; git_repository* repo = nullptr; if (git_repository_open(&repo, m_repo_path.c_str())) return result; char* refspec = "refs/heads/master"; const git_strarray refspecs = { &refspec, 1 }; git_remote* premote = NULL; if (git_remote_lookup(&premote, repo, remote.c_str()) == 0) { git_remote_callbacks callbacks; if (git_remote_init_callbacks(&callbacks, GIT_REMOTE_CALLBACKS_VERSION) == 0) { callbacks.payload = (void*) this; callbacks.credentials = cred_acquire_cb; callbacks.pack_progress = pack_progress_cb; callbacks.push_transfer_progress = push_transfer_progress_cb; callbacks.sideband_progress = sideband_progress_cb; callbacks.transfer_progress = transfer_progress_cb; git_push_options poptions; if (git_push_options_init(&poptions, GIT_PUSH_OPTIONS_VERSION) == 0) { poptions.callbacks = callbacks; result = (git_remote_push(premote, &refspecs, &poptions) == 0); if(result) { if (git_repository_state_cleanup(repo) == 0) { char* pathspec_strings[] = { "HEAD", }; git_strarray pathspec_array = { pathspec_strings, 1, }; if (git_reset_default(repo, NULL, &pathspec_array) == 0) { git_remote_disconnect(premote); } } } else { const ::git_error* e = giterr_last(); m_lastError = e->message; } } } git_remote_free(premote); } git_repository_free(repo);
解决方案
1. 修改vcpkg清单,启用libgit2的SSH特性
libgit2在vcpkg中默认未启用SSH协议支持,需要在清单的libgit2依赖项中添加features字段明确指定启用ssh特性(该特性会自动关联libssh2)。修改后的libgit2依赖项如下:
{ "name": "libgit2", "version>": "1.4.2", "features": ["ssh"] }
2. 重新安装依赖并同步项目配置
- 执行
vcpkg remove --outdated清理旧的未启用SSH的libgit2版本 - 运行
vcpkg install重新安装带SSH支持的依赖 - 在VS中重新生成项目,确保链接器正确引用新的libgit2库文件
3. 验证SSH支持是否生效
可以在代码初始化阶段添加验证逻辑,确认libgit2已加载SSH协议:
#include <git2.h> // 初始化libgit2 git_libgit2_init(); // 检查SSH协议是否注册 const git_protocol* ssh_proto = git_protocol_registered("ssh"); if (!ssh_proto) { m_lastError = "libgit2未启用SSH协议支持,请检查依赖安装"; } git_libgit2_shutdown();
内容的提问来源于stack exchange,提问作者Elias
相关产品推荐
相关产品推荐

