When I include git2.h from libgit2, I can access data structures, but not functions

StackOverflow https://stackoverflow.com/questions/16948828

  •  31-05-2022
  •  | 
  •  

Pergunta

Here is some code from Ben Straub's (link blog) that I am basing this on:

static int do_clone(const char *url, const char *path)
{
    git_repository *repo = NULL;
    int ret = git_clone(&repo, url, path, NULL);
    git_repository_free(repo);
    return ret;
}

And here is my code:

#include <git2.h>

int main(void) {
        git_repository *out = NULL;

        git_clone(&out, "https://github.com/lehitoskin/racketball", "/home/maxwell", NULL);

        return 0;
}

I am very inexperienced with C, so I apologize for having such elementary problems. Anyway, here is the error my compiler gives me:

maxwell@max-pc ~ $ gcc -I libgit2/include gitfun.c
/tmp/ccB64nPh.o: In function `main':
gitfun.c:(.text+0x31): undefined reference to `git_clone'
collect2: error: ld returned 1 exit status

Why can't I call git_clone this way?

Foi útil?

Solução

It looks like you didn't link to the library. Add -lgit2 if libgit2 is the lib name.

gcc -I libgit2/include gitfun.c -L<path to lib> -l<libname minus the "lib" part>

IOW, you compile fine but when the linker goes looking for git_clone it can't find it because you haven't specified the library that it is in.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top