2017-06-20 17 views
0

我有一个文件将被捆绑到两个共享库中。编译器创建了两个版本的库:一个用于32位类型,另一个用于64位类型。现在我需要打印诊断日志涉及的变量:针对两种不同变体处理的库的Wformat

size_t len; 

和打印语句格式说明如下:

LOG("The length is %ld",len); 

当编译器创建它抱怨一个64位版本:

format specifies type 'unsigned int' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat] 

如果我将它从%ld更改为%lu,则32位版本会提示:

format specifies type 'unsigned long' but the argument has type 'size_t' (aka 'unsigned int') [-Wformat] 

如何避免-Wformat错误(当然抑制它不是我正在寻找的解决方案)?我是否必须诉诸#ifdefs来检查编译器版本并相应地编写诊断?

编译器版本:3.8锵(安卓平台)

编辑:有人它复制到一个相关的问题。让我自为size_t似乎是常见的有一个不同的例子姿势时:

jlong val; 
LOG("val = %ld",val): 

在运行32位编译器通:

warning: format specifies type 'long' but the argument has type 'jlong' (aka 'long long') [-Wformat] 

所以,如果我试图将其更改为%LLD压制,然后警告我得到: 当运行64位传:

warning: format specifies type 'long long' but the argument has type 'jlong' (aka 'long') [-Wformat] 

如何应对这种情况?并再次重申:

编译器版本:3.8锵(安卓平台)

+0

我假设'LOG'只是一个围绕'std :: printf'的包装宏? – Rakete1111

+0

是的你是对的,它是一个包装。 – Zoso

回答

0

如果您有支持C++ 17编译器,你可以做这样的事情。

enum class Environment { bit32, bit64 }; 

#ifdef __x86_32__ 
constexpr Environment environment = Environment::bit32; 
#elif __x86_64__ 
constexpr Environment environment = Environment::bit64; 
#endif 

void print_statement() 
{  
    if constexpr (environment == Environment ::bit32) { 
    // do your 32 bit thing. 
    } 
    else if constexpr (environment == Environment ::bit64) { 
    // do your 64 bit thing. 
    } 
} 
相关问题