【问题标题】:Can you directly call functions in command line?可以直接在命令行调用函数吗?
【发布时间】:2020-09-16 05:16:42
【问题描述】:

例如,如果我有以下功能

void printText(char text [100]){
    printf("%d", text);
}

然后我可以在命令行中执行此操作

printText(Hello World)

然后得到我的预期输出为

Hello World

【问题讨论】:

  • 这在 C 语言中是不可能的。您需要有某种守护程序等待读取您的命令行输入 - 可能,这距离直截了当还有很长的路要走。
  • 动机是出于好奇,还是您有想要解决的问题?如果是这样,它是什么?可能有更好的方法来实现您所需要的。无论如何,典型的方法是在你的 func 周围编写一个带有main 的程序,并将输入作为参数传递,调用 func 并将结果打印到标准输出,在那里你可以将它通过管道传输到另一个程序(或任何你的正在尝试在这里做)。
  • text 不是int,所以printf("%d", text); 不正确
  • 我认为你需要一个 main,所以最简单的答案是创建一个程序,将你的输入作为参数然后打印出来。
  • 这是一个教师问题(在这种情况下,答案是“C 是一种无法做到这一点的编译语言。”或“命令行不是 C 解释器。”)。或者是meta.stackexchange.com/questions/66377/what-is-the-xy-problem(正如@ggorlen 已经暗示的那样)。

标签: c linux command-line


【解决方案1】:

这取决于你的外壳。一些 shell 确实支持函数。在bash、POSIX shell 以及可能的其他 shell 中,以下是正确的语法:

printText() {
   printf '%s\n' "$1"
}

printText 'Hello World'

如果您的意思是字面意思,那么不,如果不提及函数所在的文件,就不可能调用函数。用于编写函数的语言无关紧要。

但是可以编译一个 C 函数并以某种方式从 shell 调用它吗?是的。如果您从该函数创建了一个共享库(unixy 系统上的共享对象或 Windows 上的 DLL),则可以。这样做需要一个工具,但这样的工具可能会退出。 (Windows 还支持COM objects 和许多衍生技术。其中一些甚至可能使任务更容易。)

(我不知道这些工具是否真的存在或者它们是什么,因为软件推荐在 StackOverflow 上是题外话。我会说这样的工具可以围绕诸如 libffi 之类的库构建。)

【讨论】:

    【解决方案2】:

    一种解决方案是依赖dlopen()/LoadLibrary()dlsym()/GetProcAddress() 但不能保证功能 原型符合您的期望。

    更强大的解决方案在于提供一个填充的查找表 具有您知道符合预期用途的功能。

    /**
      gcc -std=c99 -o prog_c prog_c.c \
          -pedantic -Wall -Wextra -Wconversion \
          -Wc++-compat -Wwrite-strings -Wold-style-definition -Wvla \
          -g -O0 -UNDEBUG -fsanitize=address,undefined
    
      $ ./prog_c printText "Hello world"
      printText --> <Hello world>
      $ ./prog_c textLen "Hello world"
      textLen --> 11
      $ ./prog_c what "Hello world"
      cannot find function 'what'
    **/
    
    #include <stdio.h>
    #include <stdbool.h>
    #include <string.h>
    
    void
    printText(const char *text)
    {
      printf("printText --> <%s>\n", text);
    }
    
    void
    textLen(const char *text)
    {
      printf("textLen --> %d\n", (int)strlen(text));
    }
    
    typedef struct
    {
      const char *name;
      void (*fnct)(const char *);
    } TextFunction;
    
    bool // success
    call_text_function(const char *name,
                       const char *arg)
    {
      static TextFunction table[]={ {"printText", printText},
                                    {"textLen",   textLen},
                                    {NULL,        NULL} };
      for(int i=0; table[i].name!=NULL; ++i)
      {
        if(strcmp(table[i].name, name)==0)
        {
          table[i].fnct(arg);
          return true;
        }
      }
      return false;
    }
    
    int
    main(int argc,
         char **argv)
    {
      if(argc!=3)
      {
        fprintf(stderr, "usage: %s function arg\n", argv[0]);
        return 1;
      }
      if(!call_text_function(argv[1], argv[2]))
      {
        fprintf(stderr, "cannot find function '%s'\n", argv[1]);
        return 1;
      }
      return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多