c - 具有完整路径的 GCC 奇怪行为包括与搜索路径包括

标签 c gcc include

我遇到了 GCC include 行为,我正在努力理解。我提供的示例是最简单的测试代码,实际代码(和此行为)是我用来检测代码的工具的结果。我必须使用这个工具。我只是想了解出现错误的原因。有趣的是,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但无济于事。

最佳答案

在 Glibc 的 <sys/types.h> 中, int8_t 的类型定义等由

看守
#if !__GNUC_PREREQ (2, 7)

/* These types are defined by the ISO C99 header <inttypes.h>. */
# ifndef __int8_t_defined
#  define __int8_t_defined
typedef char int8_t;
typedef short int int16_t;
typedef int int32_t;
#  if __WORDSIZE == 64
typedef long int int64_t;
#  elif __GLIBC_HAVE_LONG_LONG
__extension__ typedef long long int int64_t;
#  endif
# endif

因此解决该问题的方法是在命令行上定义保护宏,传递 -D__int8_t_defined除了-DLONG_INCLUDE .

关于c - 具有完整路径的 GCC 奇怪行为包括与搜索路径包括,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11002397/

相关文章:

c - C语言中的位数据类型

c - 为什么 GCC 可以消除 C11 atomic_load 中的内存屏障?

c++ - 我如何包含具有相同包含的多个类?

C++文件编译: -L and -I arguments don't work for boost library

php - 从另一个 php 文件调用未定义的函数

c++ - 具有扩展字符 (128-255) 的 Linux 中的 open() 函数返回 -1 错误

Android 原生系统调用挂起 [C]

c - 如何使用 clang 或其他工具生成数据流图?

c++ - 在 C++ 中查找所需的库头文件是否有常见的做法?

c - Linux中C语言中Printf()在 "while(1)"循环之前不执行