Linking against libgit2 in Clion not working

217 views Asked by At

I am finding it difficult to run anything using libgit2 API in Clion. I have install everything need, using Homebrew. with this edit before

  args = std_cmake_args
  ...
  args << "-DBUILD_SHARED_LIBS=OFF"

#include <git2.h> works well but i cant seem to run it.

i tried some simple examples, it seems not to be working so i decided to take libgit2 owns example from here to see if it will work but no good news i get the following errors

/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

this is my CmakeList.txt in Clion

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})

i tried some help on libgit2 Page and changed my CmakeList.txt set() to this

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  ")

and i got the following errors: minimise i see

/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.

I am new to libgit2

I am just trying to get familiar and learn from this but can't seem to work like any ordinary API I know. All help will be appreciated.

Happy New Year!!!

1

There are 1 answers

0
Seek Addo On BEST ANSWER

I solved the issue now. Thanks to @CarlosMartínNieto, I was looking at the wrong place. I changed the CMakefileList to this:

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})

I was still getting built for x86_64 architecture being linked (i386) error so i changed the build option in the Clion preference to this

-D "CMAKE_OSX_ARCHITECTURES:STRING=x86_64"

And now everything works now. I think i will have more fun with libgit2 API now. Thanks.