如何从libgit2使用C头文件而不会出现这个错误?

我从include / git2到/ usr / include,然后试着编译下面的程序:

#include <stdio.h> #include <repository.h> int main(void) { puts("Hello, world!"); return 0; } 

当我用GCC编译这个时,我得到以下错误:

 maxwell@UNIX-PC:~$ gcc ok.c In file included from /usr/include/common.h:16:0, from /usr/include/repository.h:10, from ok.c:2: /usr/include/inttypes.h:33:2: error: #error "Use this header only with Microsoft Visual C++ compilers!" In file included from /usr/include/inttypes.h:46:0, from /usr/include/common.h:16, from /usr/include/repository.h:10, from ok.c:2: /usr/include/stdint.h:33:2: error: #error "Use this header only with Microsoft Visual C++ compilers!" In file included from /usr/include/inttypes.h:46:0, from /usr/include/common.h:16, from /usr/include/repository.h:10, from ok.c:2: /usr/include/stdint.h:89:30: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'int64_t' /usr/include/stdint.h:90:30: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'uint64_t' /usr/include/stdint.h:101:1: error: unknown type name 'uint64_t' /usr/include/stdint.h:111:1: error: unknown type name 'uint64_t' /usr/include/stdint.h:124:1: error: unknown type name 'uint64_t' In file included from /usr/include/common.h:16:0, from /usr/include/repository.h:10, from ok.c:2: /usr/include/inttypes.h:282:1: error: unknown type name '_inline' /usr/include/inttypes.h:284:11: error: expected '=', ',', ';', 'asm' or '__attribute__' before '__cdecl' /usr/include/inttypes.h:284:11: error: unknown type name '__cdecl' 

我发现只有在Visual Studio中才使用inttypes.h的错误,那么如何从使用GCC编译的程序中调用repository.h? 我真的想使用repository.h中定义的一些数据结构。 任何想法我做错了什么?

为了记录,您应该使用构建系统来安装标题。 这些头文件不会被包含在内。 如果你想使用libgit2,你应该包括git2.h

当你从include / git2复制到/ usr / include时,似乎有些头文件被覆盖。

 In file included from /usr/include/common.h:16:0, from /usr/include/repository.h:10, from ok.c:2: /usr/include/inttypes.h:33:2: error: #error "Use this header only with Microsoft Visual C++ compilers!" 

正确的做法是使用gcc的-Iinclude_path选项来包含其他头文件,并使用“-D macro_def”来定义要与预处理器一起使用的宏。

 gcc -I repository_h_path -D some_macro ok.c 

您可能想要参考gcc文档中的搜索路径 。