转自:http://blog.csdn.net/win_lin/article/details/7912693

  

例子参考高性能流媒体服务器SRS:https://github.com/winlinvip/simple-rtmp-server

SRS中广泛使用PRId64实现32和64位系统通用。

c++使用PRID64,需要两步:

  1. 包含头文件:<inttypes.h>
  2. 定义宏:__STDC_FORMAT_MACROS,可以通过编译时加-D__STDC_FORMAT_MACROS,或者在包含文件之前定义这个宏。

int64_t用来表示64位整数,在32位系统中是long long int,在64位系统中是long int,所以打印int64_t的格式化方法是:

    printf("%ld", value); // 64bit OS
printf("%lld", value); // 32bit OS

当然有跨平台的方法:

    #include <inttypes.h>
printf("%" PRId64 "\n", value);
// 相当于64位的:
printf("%" "ld" "\n", value);
// 或32位的:
printf("%" "lld" "\n", value);

其中,printf("abc" "def" “ghi")这样写多个字符串是没有问题的。

但是,死活都编译不过,错误是:error: expected ‘)’ before ‘PRId64’

找了一下这个宏的定义,/usr/include/inttypes.h:

    /* The ISO C99 standard specifies that these macros must only be
defined if explicitly requested. */
#if !defined __cplusplus || defined __STDC_FORMAT_MACROS # if __WORDSIZE ==
# define __PRI64_PREFIX "l"
# define __PRIPTR_PREFIX "l"
# else
# define __PRI64_PREFIX "ll"
# define __PRIPTR_PREFIX
# endif /* Macros for printing format specifiers. */ /* Decimal notation. */
# define PRId8 "d"
# define PRId16 "d"
# define PRId32 "d"
# define PRId64 __PRI64_PREFIX "d"

原来这个是定义给c用的,C++要用它,就要定义一个__STDC_FORMAT_MACROS宏显示打开它。

    /* test_int64.cpp
g++ -D__STDC_FORMAT_MACROS -o test_int64 -g -O0 test_int64.cpp
*/
#include <stdio.h>
#include <inttypes.h> int main(int argc, char** argv){
int64_t value = 0xFFFFFFFFFFFF;
printf("int64_t=%"PRId64", sizeof(int64_t)=%d\n", value, sizeof(int64_t));
}

编译并执行:

g++ -D__STDC_FORMAT_MACROS -o test_int64 -g -O0 test_int64.cpp

./test_int64

int64_t=281474976710655, sizeof(int64_t)=8

对于C++新标准-std=c++0x,还可以使用更好的方式:

    /* test_int64_1.cpp
g++ -o test_int64_1 -g -O0 test_int64_1.cpp
*/
#include <stdio.h>
#include <cinttypes>
using namespace std; int main(int argc, char** argv){
int64_t value = 0xFFFFFFFFFFFF;
printf("int64_t=%"PRId64", sizeof(int64_t)=%d\n", value, sizeof(int64_t));
}

不用定义那个宏了,编译和执行:

g++ -o test_int64_1 -g -O0 test_int64_1.cpp -std=c++0x

./test_int64_1

int64_t=281474976710655, sizeof(int64_t)=8

当然得指定一个新的参数:-std=c++0x,否则会报错“#error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.”

若能使用较新的g++编译,可以使用后者,否则可以用前者直接定义宏。

最新文章

  1. django 学习第一天搭建环境
  2. 深入浅出Struts2+Spring+Hibernate框架
  3. 【经验之谈】前端面试知识点总结03(JavaScript相关)——附答案
  4. Swift入门篇-集合
  5. Laravel 5 多个视图共享数据的方法
  6. OllyDbg 使用笔记 (二)
  7. C# 去除文件和文件夹的只读属性
  8. android 线程池的使用
  9. 在本机使用虚拟机安装一个linux系统,并搭建ftp服务器
  10. Maven安装问题
  11. 路飞学城-Python开发集训-第4章
  12. 按奇偶排序数组 II
  13. Netsharp配置文件
  14. 个人作业Week3
  15. FastReport的使用方法
  16. DevExpress WPF入门指南:DXWindow应用
  17. 互评Beta版本——二次元梦之队——“I Do”
  18. 使用npm uninstall卸载express无效
  19. 分享Kali Linux 2017年第31周镜像文件
  20. spring boot 添加自定义属性

热门文章

  1. 【转】Comet:基于 HTTP 长连接的“服务器推”技术
  2. SSM java.lang.NullPointerException
  3. [luoguP2146] 软件包管理器(树链剖分)
  4. boot简介
  5. MT6753/MT6755 呼吸灯功能添加
  6. [NOIP2008] 提高组 洛谷P1125 笨小猴
  7. AjaxFileUpload文件上传组件(php+jQuery+ajax)
  8. hnuun 11544 小明的烦恼——找字符串(求环形字符串的最小最大字典序)
  9. POJ 3723 Conscription【最小生成树】
  10. java代码判断文件类型(判断文件后缀名)