C 在头文件中定义一个指向外部函数的宏函数

C define a macro function in header file pointing to an extern function

我对C中的函数宏定义有疑问(我是初学者): 我有一个使用 strtok() 函数的 COTS 库 lib.a,但我的 CERT 应用程序只支持 strtok_r,所以我在编译时遇到错误。

我如何在头文件中定义 strtok 函数应该被 strtok_r 覆盖?

我试过类似的方法,但出现错误:

extern char *strtok_r(char *s1, const char *s2, char **saveptr);
#define strtok(s1,s2) strtok_r(s1,s2,saveptr)

哪种方法是实现结果的最佳且简洁的方法?

非常感谢您的意见。

您不能将 strtok_r 重新定义为 strtok,因为随着参数数量的不同,它们的操作也不同。

char *strtok(c​​har *restrict, const char *restrict);

char *strtok_r(char *, const char *, char **);

如果你没有 strtok_r 那么写一个是最好的方法。

一位评论员解释说您希望从 strtok_r 实现 strtok,所以我也包括了它的代码。

这是 strtok_r() 来自:WoBoq

/* Reentrant string tokenizer.  Generic version.
   Copyright (C) 1991-2018 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <string.h>

#ifndef _LIBC
/* Get specification.  */
# include "strtok_r.h"
# define __strtok_r strtok_r
#endif

/* Parse S into tokens separated by characters in DELIM.
   If S is NULL, the saved pointer in SAVE_PTR is used as
   the next starting point.  For example:
        char s[] = "-abc-=-def";
        char *sp;
        x = strtok_r(s, "-", &sp);        // x = "abc", sp = "=-def"
        x = strtok_r(NULL, "-=", &sp);        // x = "def", sp = NULL
        x = strtok_r(NULL, "=", &sp);        // x = NULL
                // s = "abc[=10=]-def[=10=]"
*/

char *
__strtok_r (char *s, const char *delim, char **save_ptr)
{
  char *end;

  if (s == NULL)
    s = *save_ptr;

  if (*s == '[=10=]')
    {
      *save_ptr = s;
      return NULL;
    }

  /* Scan leading delimiters.  */
  s += strspn (s, delim);
  if (*s == '[=10=]')
    {
      *save_ptr = s;
      return NULL;
    }

  /* Find the end of the token.  */
  end = s + strcspn (s, delim);
  if (*end == '[=10=]')
    {
      *save_ptr = end;
      return s;
    }

  /* Terminate the token and make *SAVE_PTR point past it.  */
  *end = '[=10=]';
  *save_ptr = end + 1;
  return s;
}
#ifdef weak_alias
libc_hidden_def (__strtok_r)
weak_alias (__strtok_r, strtok_r)
#endif

这是 strtok() 来自:glibc/blob/master/string/strtok.c

/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <string.h>

/* Parse S into tokens separated by characters in DELIM.
   If S is NULL, the last string strtok() was called with is
   used.  For example:
    char s[] = "-abc-=-def";
    x = strtok(s, "-");     // x = "abc"
    x = strtok(NULL, "-=");     // x = "def"
    x = strtok(NULL, "=");      // x = NULL
        // s = "abc[=11=]=-def[=11=]"
*/
char *
strtok (char *s, const char *delim)
{
  static char *olds;
  return __strtok_r (s, delim, &olds);
}

这是每个函数的一个示例实现。有许多包含该库的编译器源可用。如果该许可证不符合您的要求,请尝试搜索另一个符合您需要的条款或编写您自己的函数。

只需将该源写入一个单独的文件并将其添加到您的 link 列表中。

在这个 GeeksForGeeks 网页“strtok() and strtok_r() functions in C with examples”上解释和演示了尝试重新定义这些函数的问题。 strtok_r 函数需要为后续调用提供的参数不同于为第一次调用提供的参数。不方便做一个define,用一个全局的,可以决定是设置还是测试。

假设我理解正确:

  1. 您没有可用的 strtok 功能。
  2. 您使用的库 libother.a(只是给它起个名字)在内部使用 strtok - 函数。
  3. 您没有这个库的源代码。

Rob 走在正确的轨道上,但我认为走错了路...

您可以像这样包装 strtok_r 函数来构建自己的 strtok 函数:

将此函数插入​​您的 源文件之一:

char *strtok(char *s1, const char *delim) {
    static char* saveptr = 0;
    return strtok_r(s1, delim, &saveptr);
}

然后像

这样的通常方式编译和链接你的代码
gcc your_code_file.c -lother -o your_binary

应该做的。 但是:有一个理由说明为什么您应该更喜欢使用 strtok_r 而不是 strtokstrtok 一次只能解析一个字符串,strtok_r 允许同时解析任意数量的不同字符串。

上面的 'hack' 你失去了 strtok_r 的优势。