vs2015升级旧工程报snprintf已有主体及“int8_t” 重定义

我现在用的VisualStudio 最新版本 visual studio 2015,按提示将旧工程转成了vs2015格式。

然后编译,报出无数错误。。。

c:\program files (x86)\microsoft visual studio 14.0\vc\include\stdint.h(17): error C2371: “int8_t”: 重定义;不同的基类型

函数“int snprintf(char *const ,const size_t,const char *const ,…)”已有主体

。。。。。

 

看到上面的一大堆报错,脑子一下子就晕了,尼玛,什么玩意儿,编译器支持这么差?没有说明不支持vs编译器啊。
仔细分析上面的错误信息,发现主要就是snprintfint8_ttimespec重定义,

考虑到vs2015比较新,用google搜索了一下”tcmalloc vs2010”,发现了这篇文章《TCMalloc static lib in vs2010》

文中解决的问题我并不关心,但我发现了这个:(#define WIN32_OVERRIDE_ALLOCATORS in config.h).

在项目工程下面果然发现找到了文中提到的config.h以及提到的宏定义WIN32_OVERRIDE_ALLOCATORS

config.h通过宏定义来控制代码生成。在不同的编译器下编译,要根据编译报错的信息来相应修改config.h来解决。上图中最后一行,就是关于snprintf的

/* Define to 1 if your libc has a snprintf implementation */
#undef HAVE_SNPRINTF

根据注释的说明,如果编译已经有snprintf 的实现,就要将HAVE_SNPRINTF定义为1

config.h中找到下面的定义

/* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H

改为

/* Define to 1 if you have the <stdint.h> header file. */
#define HAVE_STDINT_H 1

这是port.h中的关于timespec的代码片段,看代码注释,是因为mingw没有定义timespc,而且mingw64中有定义,所以有点混乱,所以在这里用_TIMESPEC_DEFINED来做一个保护

// mingw64 seems to define timespec (though mingw.org mingw doesn't),
// protected by the _TIMESPEC_DEFINED macro.
#ifndef _TIMESPEC_DEFINED
struct timespec {
  int tv_sec;
  int tv_nsec;
};
#endif

而在<time.h>是通过_CRT_NO_TIME_T来控制是否定义timespec

#ifndef _CRT_NO_TIME_T
    struct timespec
    {
        time_t tv_sec;  // Seconds - >= 0
        long   tv_nsec; // Nanoseconds - [0, 999999999]
    };
#endif

所以在config.h中加入下面一行,以去掉time.h中的timespec定义

#define _CRT_NO_TIME_T 1

最后保存config.h再编译,就可以通过了。

但是,编译通过只是第一步,是否能真的正常使用,还有待后面工作进行验证。

结论

要正确编译tcmalloc,应该根据c++编译器类型和版本的不同,修改config.h以达到与编译工具最匹配的状态,config.h中还有很多选项没有仔细研究,需要进一步深入了解。

posted on 2018-01-22 09:42  &大飞  阅读(1433)  评论(0编辑  收藏  举报

导航