vs2015 中无法链接strcasecmp 和 strncasecmp的解决办法

原因:strcasecmp是BSD/POSIX标准,非ANSI标准,所以微软并不支持。

strcasecmp最早出现在4.4BSD,后来加入到Posix标准里。MS的一直用_stricmp函数,功能完全一样。

方案一:stricmp替换strcasecmp ;strnicmp替换strncasecmp,即可。

之后貌似还会报错:

   error C4996: 'strnicmp': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strnicmp. See online         help for details. D:\CAFFE_ROOT\src\caffe\common.cpp

解决办法:

这个问题在VS 2012之前的版本中是不会当做错误的,只是提出一个警告。为了避免报错,可以使用以下两个宏定义来屏蔽掉这种        错误。

在common.cpp文件的属性->c/c++->预处理器->预处理器定义   中添加:

    _CRT_SECURE_NO_DEPRECATE 

扫描二维码关注公众号,回复: 2228922 查看本文章

    _CRT_NONSTDC_NO_DEPRECATE 

方案二:用VC SDK中的函数来代替,在main函数外面步添加一下代码即可:

#ifdef _MSC_VER
#define strcasecmp stricmp
#define strncasecmp  strnicmp 
#endif

方案三:自己添加该函数的声明和定义

第一步:.h文件添加:

#ifdef _MSC_VER
int strcasecmp(char *s1, char *s2);
int strncasecmp(char *s1, char *s2, register int n);
#endif
第二部:.c文件中添加
#ifdef _MSC_VER
int strcasecmp(char *s1, char *s2)
{
   while  (toupper((unsigned char)*s1) == toupper((unsigned char)*s2++))
       if (*s1++ == '') return 0;
   return(toupper((unsigned char)*s1) - toupper((unsigned char)*--s2));
}

int strncasecmp(char *s1, char *s2, register int n)
{
  while (--n >= 0 && toupper((unsigned char)*s1) == toupper((unsigned char)*s2++))
      if (*s1++ == '')  return 0;
  return(n < 0 ? 0 : toupper((unsigned char)*s1) - toupper((unsigned char)*--s2));
}
#endif



猜你喜欢

转载自blog.csdn.net/mijichui2153/article/details/81061067