【问题标题】:Portable typedefs for ANSI CANSI C 的可移植类型定义
【发布时间】:2014-02-11 13:57:53
【问题描述】:

我在项目的标头中有一个可移植的类型定义,我的目标是在多个平台上编译。我正在使用以下类型定义:

#ifndef PLATFORM_H
#define PLATFORM_H

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

#ifndef TRUE
#define TRUE (1)
#endif

#ifndef FALSE
#define FALSE (0)
#endif

typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed int int16_t;
typedef unsigned int uint16_t;
typedef signed long int int32_t;
typedef unsigned long int uint32_t;
typedef signed long long int int64_t;
typedef unsigned long long int uint64_t;

#endif

此标头是特定于平台的,因此我对每个平台都有类似的声明,以确保整数始终相同。但是,在编译时,我得到了这个:

conflicting types for ‘int32_t’
In file included from /usr/include/stdlib.h:314:0,
                 from platform.h:5,
                 from main.c:1:
/usr/include/x86_64-linux-gnu/sys/types.h:196:1: note: previous declaration of ‘int32_t’ was here

和其他错误。可能是什么原因?我可以覆盖 typedef,还是先检查它是否存在?

【问题讨论】:

    标签: c portability


    【解决方案1】:

    您收到错误消息,因为您为 typedefs 选择的名称与 &lt;stdint.h&gt; 标头中的类型名称冲突。

    由于您正在寻找已在 stdint.h 中定义的类型,您不妨将其包含在提供此标头的平台上。

    【讨论】:

    • 在其他平台中包含 stdint.h 吗?但是如果平台不支持 C99 怎么办?
    • @vinnylinux 由于您的标头是特定于平台的,因此在不提供 stdint.h 的平台上,您将继续使用您自己的标头中的定义。
    • 请注意,我不包括 stdint.h,只包括 stdlib
    • 包含stdint.h后,我得到:错误:未知类型名称'uint16_t'
    • 我建议在定义这些类型之前测试它们是否存在,使用 CMake 或 autoconf 等配置工具
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 2016-07-08
    • 2012-06-25
    • 2015-11-19
    • 2015-02-24
    相关资源
    最近更新 更多