Incomplete typedefs in libgit2


I am trying to integrate libgit2 (1.5.0) into my project. The compilation failes due to missing type definitions.

The CMakeLists.txt of my project includes the include directory and the libgit2.a file:

target_include_directories(myprj PUBLIC ../libgit2/src/libgit2-1.5.0/include)

set(myprj "${CMAKE_SOURCE_DIR}/lib/libgit2.a")
link_directories(myprj ${PROJECT_SOURCE_DIR}/lib)
target_link_libraries(myprj PUBLIC ${libgit} crypto ssl)

The following code is a simplified version of the code I am using, but it should demonstrate the problem:

#include <git2.h>

git_reference *ref;
git_branch_t btype;

int code = git_branch_next(&ref, &btype, iterator);
if(code != 0)
    return code;

char* name = ref->name;

The compiler fails with the error:

error: invalid use of incomplete typedef ‘git_reference’
char* name = ref->name

The include directory defines the type but the stuct itself is defined in the src directory only.

libgit2-1.5.0/include/git2/types.h (source)

typedef struct git_reference git_reference;


struct git_reference {

I have tried to fix it by importing the src dir into my project but this doesnt work because of missing includes.

Import in Cmake:

target_include_directories(myprj PUBLIC ../libgit2/src/libgit2-1.5.0/src/libgit2)


libgit2/src/libgit2-1.5.0/src/libgit2/common.h:10:10: fatal error: git2_util.h: File or directory not found
   10 | #include "git2_util.h"

>Solution :

I think this is on purpose, to make git_reference an opaque type. It can then be changed at will by the library authors without breaking compatibility.

To get the name of a reference, use git_reference_name instead of accessing the field directly:

const char* name = git_reference_name(ref);

Leave a ReplyCancel reply