我遇到了我试图理解的 GCC 包含行为。我提供的示例是最简单的测试代码,实际代码(以及此行为)是我用来检测代码的工具的结果。我必须使用这个工具。我只是想了解出现错误的原因。有趣的是,g++ 工作正常。这是示例:
如果我包含<sys/types.h>
所有编译都很好,但如果我包含"/usr/include/sys/types.h"
然后我得到一个错误。
这是我运行gcc
下面包含完整路径的第一个命令时出现的错误:
In file included from hello.c:7:
/usr/include/sys/types.h:195: error: redefinition of typedef ‘int8_t’
hello.c:5: error: previous declaration of ‘int8_t’ was here
编译器命令,使用 GCC 4.1.2 (CentOS 5) 导致错误:
gcc -g -I. --verbose -c -o hello.o -DLONG_INCLUDE hello.c
或者这个不会导致错误的
gcc -g -I. --verbose -c -o hello.o hello.c
代码:
/* hello2.h */
#ifdef _cplusplus
extern "C" {
#endif
int myFunc(int *a);
#ifdef _cplusplus
}
#endif
/* hello.c */
#include <stdio.h>
#include <string.h>
typedef signed char int8_t;
#ifdef LONG_INCLUDE
#include "/usr/include/sys/types.h"
#else
#include <sys/types.h>
#endif
#include "hello.h"
int myFunc(int *a)
{
if (a == NULL)
return -1;
int b = *a;
b += 20;
if (b > 80)
b = 80;
return b;
}
谢谢
更新:
在通过gcc -E
它看起来指定完整路径时查看预处理器输出后,gcc 不会将其视为系统包含路径,并且不知何故,正在促成(导致?)错误。尝试使用该-isystem
选项/usr/include
但/usr/include/sys
无济于事。