C++ g++ 错误:“stricmp”未在此范围内声明(但“strcmp”可以)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1784767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:09:51  来源:igfitidea点击:

g++ error: ‘stricmp’ was not declared in this scope (but OK for 'strcmp')

c++g++

提问by Gonzalo

I am trying to compile the following very very simple piece of source code:

我正在尝试编译以下非常简单的源代码:

#include <cstring>
// #include <string.h>
// using namespace std;

class Helper {
public:
    int cStringsAreEqual(const char *s1, const char *s2) {
        return stricmp(s1, s2);
    }
};

... but I am getting the following error message:

...但我收到以下错误消息:

   g++ error: ‘stricmp' was not declared in this scope

However when I use strcmp()instead of stricmp()then everything is fine!

但是,当我使用strcmp()而不是stricmp() 时,一切都很好!

What can be wrong here? Shouldn't stricmp() be allowed when strcmp() is allowed?

这里有什么问题?当允许 strcmp() 时不应该允许 stricmp() 吗?

Sureley, this all could be written in a much better way without using strcmp/stricmp.

Sureley,这一切都可以在不使用 strcmp/stricmp 的情况下以更好的方式编写。

But that's not the point here.

但这不是重点。

I am porting a piece of software - which makes much use of calls to stricmp(). And if somehow possible I would like to avoid all of the efforts needed to change every call to stricmp.

我正在移植一个软件——它大量使用了对 stricmp() 的调用。如果可能的话,我想避免所有更改对 stricmp 的调用所需的所有努力。

Any help on this would be very much appreciated!

对此的任何帮助将不胜感激!

BTW: I am using Ubuntu karmic OS (v9.10) with g++ v4.4.1.

顺便说一句:我正在使用带有 g++ v4.4.1 的 Ubuntu karmic OS (v9.10)。

BTW: as you can see I also made some trials with '#include string.h' or with 'namespace std' but nothing helped.

顺便说一句:正如你所看到的,我也用 '#include string.h' 或 'namespace std' 做了一些试验,但没有任何帮助。

回答by Gonzalo

Try strcasecmp(). Here's the manual pagefor it. It is conforming to 4.4BSD and POSIX.1-2001.

试试strcasecmp()。这是它的手册页。它符合 4.4BSD 和 POSIX.1-2001。

回答by Mark Rushakoff

stricmpis neither POSIX nor ANSI, so it doesn't really matter if strcmpis allowed, if your compiler or standard library is strictly adhering to POSIX or ANSI standard library functions (as is probably the case with the GCC suite).

stricmp既不是 POSIX 也不是 ANSI,因此strcmp如果您的编译器或标准库严格遵守 POSIX 或 ANSI 标准库函数(GCC 套件可能就是这种情况),那么是否允许并不重要。

回答by Ryan Christensen

Add a define for it to overwrite stricmp with strcasecmp on the platforms you are looking for.

为它添加一个定义,以在您正在寻找的平台上用 strcasecmp 覆盖 stricmp。

#ifdef _IPHONE <- your platform define here
#define stricmp strcasecmp
#define strnicmp strncasecmp
#endif

Then you can just use stricmp always.

然后你可以一直使用 stricmp 。

回答by Gary

Pretty easy to make your own if need be...

如果需要,很容易制作自己的...

int my_stricmp (const char *s1, const char *s2)
{
    while (*s1 != 0 && *s2 != 0)
    {
        if (*s1 != *s2 && ::toupper (*s1) != ::toupper(*s2))
        {
            return -1;
        }
        s1++;
        s2++;
    }
    return (*s1 == 0 && *s2 == 0) ? 0 : -1;
}