在 Clion 中链接 libgit2 不起作用
Posted
技术标签:
【中文标题】在 Clion 中链接 libgit2 不起作用【英文标题】:Linking against libgit2 in Clion not working 【发布时间】:2016-12-31 18:17:44 【问题描述】:我发现在 Clion 中使用 libgit2 API 运行任何东西都很困难。我已经使用 Homebrew 安装了所有需要的东西。之前有此编辑
args = std_cmake_args
...
args << "-DBUILD_SHARED_LIBS=OFF"
#include <git2.h>
运行良好,但我似乎无法运行它。
我尝试了一些简单的示例,但似乎不起作用,所以我决定从 here 获取 libgit2 拥有的示例,看看它是否会起作用,但没有好消息 我收到以下错误
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/addo/ClionProjects/Libgit2Training/cmake-build-debug --target Libgit2Training -- -j 4 [ 50%] Building C object CMakeFiles/Libgit2Training.dir/main.c.o [100%] Linking C executable Libgit2Training Undefined symbols for architecture i386: "_git_blob_lookup", referenced from:
_blob_parsing in main.c.o "_git_blob_rawcontent", referenced from:
_blob_parsing in main.c.o "_git_blob_rawsize", referenced from:
_blob_parsing in main.c.o "_git_commit_author", referenced from:
_commit_parsing in main.c.o
_revwalking in main.c.o "_git_commit_committer", referenced from:
_commit_parsing in main.c.o "_git_commit_create_v", referenced from:
_commit_writing in main.c.o "_git_commit_free", referenced from:
_commit_parsing in main.c.o
_tag_parsing in main.c.o
_revwalking in main.c.o "_git_commit_id", referenced from:
_commit_parsing in main.c.o "_git_commit_lookup", referenced from:
_commit_writing in main.c.o
_commit_parsing in main.c.o
_revwalking in main.c.o "_git_commit_message", referenced from:
_commit_parsing in main.c.o
_revwalking in main.c.o "_git_commit_parent", referenced from:
_commit_parsing in main.c.o "_git_commit_parentcount", referenced from:
_commit_parsing in main.c.o "_git_commit_time", referenced from:
_commit_parsing in main.c.o "_git_config_get_int32", referenced from:
_config_files in main.c.o "_git_config_get_string", referenced from:
_config_files in main.c.o "_git_config_open_ondisk", referenced from:
_config_files in main.c.o "_git_index_entrycount", referenced from:
_index_walking in main.c.o "_git_index_free", referenced from:
_index_walking in main.c.o "_git_index_get_byindex", referenced from:
_index_walking in main.c.o "_git_libgit2_init", referenced from:
_main in main.c.o "_git_libgit2_shutdown", referenced from:
_main in main.c.o "_git_object_free", referenced from:
_tree_parsing in main.c.o "_git_object_type2string", referenced from:
_object_database in main.c.o
_tag_parsing in main.c.o "_git_odb_object_data", referenced from:
_object_database in main.c.o "_git_odb_object_free", referenced from:
_object_database in main.c.o "_git_odb_object_size", referenced from:
_object_database in main.c.o "_git_odb_object_type", referenced from:
_object_database in main.c.o "_git_odb_read", referenced from:
_object_database in main.c.o "_git_odb_write", referenced from:
_object_database in main.c.o "_git_oid_fmt", referenced from:
_oid_parsing in main.c.o
_object_database in main.c.o
_commit_writing in main.c.o
_commit_parsing in main.c.o
_reference_listing in main.c.o "_git_oid_fromstr", referenced from:
_oid_parsing in main.c.o
_commit_writing in main.c.o
_commit_parsing in main.c.o
_tag_parsing in main.c.o
_tree_parsing in main.c.o
_blob_parsing in main.c.o
_revwalking in main.c.o
... "_git_reference_list", referenced from:
_reference_listing in main.c.o "_git_reference_lookup", referenced from:
_reference_listing in main.c.o "_git_reference_symbolic_target", referenced from:
_reference_listing in main.c.o "_git_reference_target", referenced from:
_reference_listing in main.c.o "_git_reference_type", referenced from:
_reference_listing in main.c.o "_git_repository_free", referenced from:
_main in main.c.o "_git_repository_index", referenced from:
_index_walking in main.c.o "_git_repository_odb", referenced from:
_object_database in main.c.o "_git_repository_open", referenced from:
_main in main.c.o "_git_revwalk_free", referenced from:
_revwalking in main.c.o "_git_revwalk_new", referenced from:
_revwalking in main.c.o "_git_revwalk_next", referenced from:
_revwalking in main.c.o "_git_revwalk_push", referenced from:
_revwalking in main.c.o "_git_revwalk_sorting", referenced from:
_revwalking in main.c.o "_git_signature_new", referenced from:
_commit_writing in main.c.o "_git_strarray_free", referenced from:
_reference_listing in main.c.o "_git_tag_lookup", referenced from:
_tag_parsing in main.c.o "_git_tag_message", referenced from:
_tag_parsing in main.c.o "_git_tag_name", referenced from:
_tag_parsing in main.c.o "_git_tag_target", referenced from:
_tag_parsing in main.c.o "_git_tag_target_type", referenced from:
_tag_parsing in main.c.o "_git_tree_entry_byindex", referenced from:
_tree_parsing in main.c.o "_git_tree_entry_byname", referenced from:
_tree_parsing in main.c.o "_git_tree_entry_name", referenced from:
_tree_parsing in main.c.o "_git_tree_entry_to_object", referenced from:
_tree_parsing in main.c.o "_git_tree_entrycount", referenced from:
_tree_parsing in main.c.o "_git_tree_lookup", referenced from:
_commit_writing in main.c.o
_tree_parsing in main.c.o "_giterr_last", referenced from:
_check_error in main.c.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation) make[3]: *** [Libgit2Training] Error 1 make[2]: *** [CMakeFiles/Libgit2Training.dir/all] Error 2 make[1]: *** [CMakeFiles/Libgit2Training.dir/rule] Error 2 make: *** [Libgit2Training] Error 2
这是我在 Clion 中的 CmakeList.txt
cmake_minimum_required(VERSION 3.6)
project(Libgit2Training)
#set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_FLAGS "$CMAKE_C_FLAGS -Wall -std=c11 ")
set(SOURCE_FILES main.c)
add_executable(Libgit2Training $SOURCE_FILES)
我在libgit2
页面上尝试了一些帮助,并将我的CmakeList.txt
set() 更改为此
set(CMAKE_C_FLAGS "$CMAKE_C_FLAGS -Wall -std=c11
CFLAGS += -I/usr/local/Cellar/libgit2/include
LDFLAGS += -L/usr/local/Cellar/libgit2/binaries
LIBRARIES += -lgit2 ")
我得到以下错误:最小化我看到
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/addo/ClionProjects/Libgit2Training/cmake-build-debug --target Libgit2Training -- -j 4
[ 50%] Building C object CMakeFiles/Libgit2Training.dir/main.c.o
[100%] Linking C executable Libgit2Training
clang: error: no input files
make[3]: *** [Libgit2Training] Error 1
make[2]: *** [CMakeFiles/Libgit2Training.dir/all] Error 2
make[1]: *** [CMakeFiles/Libgit2Training.dir/rule] Error 2
make: *** [Libgit2Training] Error 2
seemed not to parse in a .c file.
我是 libgit2 的新手
我只是想熟悉并从中学习,但似乎无法像我所知道的任何普通 API 那样工作。我们将不胜感激。
新年快乐!!!
【问题讨论】:
为什么要禁用共享库?你是怎么想出那个字符串来设置为CMAKE_C_FLAGS
的?这在 libgit2 链接指南中没有提到,它们不是用于 Homebrew 的正确路径,而且也不是你告诉 C 编译器链接它的方式。看来您的问题非常笼统,关于 CMake、C 编译器和环境变量的工作方式,而不是任何特定于 libgit2 的问题。
@CarlosMartínNieto 我禁用了共享库,因为我想让它们在 mac 上作为静态而不是 dyllib。我从 libgit2 主页 [这里][libgit2.github.com/docs/guides/build-and-link/] 获得了字符串,我刚刚启用了共享库并重新安装并从零开始构建所有内容,但我仍然得到架构 i386 的未定义符号:“_git_blob_lookup”,引用自:_blob_parsing in main。 c.o
@CarlosMartínNieto 你建议我在这一点上做什么?我应该从 Clion 构建首选项中链接它还是可以在 CMakeList.txt 文件中进行链接。
【参考方案1】:
我现在解决了这个问题。感谢@CarlosMartínNieto,我看错了地方。我将 CMakefileList 更改为:
cmake_minimum_required(VERSION 3.6)
project(Libgit2Training)
#set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_FLAGS "$CMAKE_C_FLAGS -Wall -std=c11 -L/opt/local/lib -lgit2 -lssh2")
/opt/local/lib # for macports
set(SOURCE_FILES main.c)
add_executable(Libgit2Training $SOURCE_FILES)
我仍在为 x86_64 架构被链接 (i386) 错误而构建,因此我将 Clion
首选项中的构建选项更改为此
-D "CMAKE_OSX_ARCHITECTURES:STRING=x86_64"
现在一切正常。我想我现在会对 libgit2 API 有更多的乐趣。谢谢。
【讨论】:
以上是关于在 Clion 中链接 libgit2 不起作用的主要内容,如果未能解决你的问题,请参考以下文章
社交图标在移动设备和某些平板电脑中不起作用(链接不起作用,它们不会在悬停时更改图像)
在 ARM 平台上为嵌入式 Linux 交叉编译 Clion