我发现在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一样工作。所有帮助将不胜感激。

新年快乐!!!

最佳答案

我现在解决了这个问题。感谢@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})


我仍在为链接(i386)错误的x86_64体系结构而构建,因此我将Clion首选项中的build选项更改为此

-D "CMAKE_OSX_ARCHITECTURES:STRING=x86_64"


现在一切正常。我想我现在会更喜欢libgit2 API。谢谢。

关于c - 在Clion中与libgit2链接无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41410719/

10-14 15:41