在c语言中调用运行时函数

在c语言中调用运行时函数,c,function-pointers,C,Function Pointers,如何在运行时调用函数?我想知道我是否可以不用if或开关来调用它 if(arg[1] == something) //Now what I'm looking for call_function; 下面是我正在寻找的,它使用arg调用函数print #include <stdio.h> void print() { printf("print function called\n"); } int main(int argc, char *argv[]) {

如何在运行时调用函数?我想知道我是否可以不用if开关来调用它

if(arg[1] == something) //Now what I'm looking for
      call_function;
下面是我正在寻找的,它使用arg调用函数print

#include <stdio.h>

void print()
{

    printf("print function called\n");
}

int main(int argc, char *argv[])
{
    argv[1] to call the print function 
}

这不是琐碎的或自动的。函数名只存在于语言中,而不存在于正在执行的程序中。您必须自己提供从合适的字符串常量到函数的映射(或者在Posix上使用一些现有机制,如
dlsym
)。您可以通过函数指针引用函数:

这只是为了说明;您应该实现一些合适的函数指针值关联查找结构,而不是条件语句序列


(C++中,这是一个代码> STD::unOrdEdEdMult。在C中,你必须找到你自己的.< /P> < P>这不是微不足道的或是自动的。函数名只存在于语言中,而不存在于正在执行的程序中。您必须自己提供从合适的字符串常量到函数的映射(或者在Posix上使用一些现有机制,如

dlsym
)。您可以通过函数指针引用函数:

这只是为了说明;您应该实现一些合适的函数指针值关联查找结构,而不是条件语句序列


<>(在C++中,这是一个代码> STD::unOrdEdEdMult。在C中,你必须找到你自己的.< /P>< P>如果你不寻找一个“纯C”解决方案,那么你可以使用你的OS的API来为你做映射。在POSIX下(编译并运行):

#包括
#包括
无效foo(无效)
{
printf(“foo\n”);
}
空心条(空心)
{
printf(“bar\n”);
}
无效baz(无效)
{
printf(“baz\n”);
}
int main(int argc,char*argv[])
{
void*hndl=dlopen(NULL,RTLD_);
无效(*fn)(无效)=dlsym(hndl,argv[1]);
如果(fn){
fn();
}否则{
printf(“未找到函数%s()”,argv[1]);
}
返回0;
}

如果您不是在寻找“纯C”解决方案,那么很可能您可以使用操作系统的API为您进行映射。在POSIX下(编译并运行):

#包括
#包括
无效foo(无效)
{
printf(“foo\n”);
}
空心条(空心)
{
printf(“bar\n”);
}
无效baz(无效)
{
printf(“baz\n”);
}
int main(int argc,char*argv[])
{
void*hndl=dlopen(NULL,RTLD_);
无效(*fn)(无效)=dlsym(hndl,argv[1]);
如果(fn){
fn();
}否则{
printf(“未找到函数%s()”,argv[1]);
}
返回0;
}

事实上,有一种方法

在运行时,您可以编写一个包含函数定义的.c文件,主要是编写对
argv[1]
中命名函数的调用。然后,您可以调用编译器(gcc-linux或cl-windows或任何东西)(通过
system(command)
)生成可执行文件。然后调用该可执行文件(通过
系统(命令)
)并查看完成的工作

什么?我没有说这是一种实用的方式,我只是说这是一种方式


证明这确实有效的证据:

#include <stdio.h>
#include <stdlib.h>

const char file_includes[] = "#include <stdio.h>\n";
const char file_functions[] = 
  "void print() {\n"
  "    printf(\"print function called\\n\");\n"
  "}\n"
  "void print2() {\n"
  "    printf(\"second print function called\\n\");\n"
  "}\n";

const char file_main_prolog[] = "int main() {\n";
const char file_main_epilog[] = "  return 0;\n}\n";


int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("invalid program arguments\n");
    return 1;
  }

  FILE *fout = fopen("function_call.c", "w");

  fprintf(fout, "%s", file_includes);
  fprintf(fout, "%s", file_functions);
  fprintf(fout, "%s", file_main_prolog);
  fprintf(fout, "  %s();\n", argv[1]);
  fprintf(fout, "%s", file_main_epilog);

  fclose(fout);

  system("gcc -o function_call function_call.c");
  system("./function_call");

  return 0;
}
#包括
#包括
const char file_includes[]=“#includes\n”;
常量字符文件_函数[]=
“无效打印(){\n”
printf(\“调用的打印函数\\n\”);\n
“}\n”
“void print2(){\n”
printf(\“调用的第二个打印函数\\n\”);\n
“}\n”;
const char file_main_prolog[]=“int main(){\n”;
常量字符文件\u main\u epilog[]=“返回0;\n}\n”;
int main(int argc,char*argv[]){
如果(argc<2){
printf(“无效的程序参数\n”);
返回1;
}
FILE*fout=fopen(“function_call.c”、“w”);
fprintf(fout,“%s”,包括文件);
fprintf(fout,“%s”,文件函数);
fprintf(fout,“%s”,文件\u main\u prolog);
fprintf(fout,“%s();\n”,argv[1]);
fprintf(fout,“%s”,文件\u main\u epilog);
fclose(fout);
系统(“gcc-o函数调用函数调用c”);
系统(“/功能调用”);
返回0;
}

这在安装了gcc的linux下工作。您可以使用参数
print
print2
运行此程序,您将分别得到名为
打印函数或名为
的第二个打印函数。

嗯,实际上有一种方法

在运行时,您可以编写一个包含函数定义的.c文件,主要是编写对
argv[1]
中命名函数的调用。然后,您可以调用编译器(gcc-linux或cl-windows或任何东西)(通过
system(command)
)生成可执行文件。然后调用该可执行文件(通过
系统(命令)
)并查看完成的工作

什么?我没有说这是一种实用的方式,我只是说这是一种方式


证明这确实有效的证据:

#include <stdio.h>
#include <stdlib.h>

const char file_includes[] = "#include <stdio.h>\n";
const char file_functions[] = 
  "void print() {\n"
  "    printf(\"print function called\\n\");\n"
  "}\n"
  "void print2() {\n"
  "    printf(\"second print function called\\n\");\n"
  "}\n";

const char file_main_prolog[] = "int main() {\n";
const char file_main_epilog[] = "  return 0;\n}\n";


int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("invalid program arguments\n");
    return 1;
  }

  FILE *fout = fopen("function_call.c", "w");

  fprintf(fout, "%s", file_includes);
  fprintf(fout, "%s", file_functions);
  fprintf(fout, "%s", file_main_prolog);
  fprintf(fout, "  %s();\n", argv[1]);
  fprintf(fout, "%s", file_main_epilog);

  fclose(fout);

  system("gcc -o function_call function_call.c");
  system("./function_call");

  return 0;
}
#包括
#包括
const char file_includes[]=“#includes\n”;
常量字符文件_函数[]=
“无效打印(){\n”
printf(\“调用的打印函数\\n\”);\n
“}\n”
“void print2(){\n”
printf(\“调用的第二个打印函数\\n\”);\n
“}\n”;
const char file_main_prolog[]=“int main(){\n”;
常量字符文件\u main\u epilog[]=“返回0;\n}\n”;
int main(int argc,char*argv[]){
如果(argc<2){
printf(“无效的程序参数\n”);
返回1;
}
FILE*fout=fopen(“function_call.c”、“w”);
fprintf(fout,“%s”,包括文件);
fprintf(fout,“%s”,文件函数);
fprintf(fout,“%s”,文件\u main\u prolog);
fprintf(fout,“%s();\n”,argv[1]);
fprintf(fout,“%s”,文件\u main\u epilog);
fclose(fout);
系统(“gcc-o函数调用函数调用c”);
系统(“/功能调用”);
返回0;
}

这在安装了gcc的linux下工作。您可以使用参数
print
print2
运行此程序,您将分别获得名为
打印函数或名为
第二个打印函数。

请参阅。请参阅。“函数名仅存在于语言中,而不存在于执行程序中”-如果
strip
。如果你不这样做,那么非
静态
有趣
#include <stdio.h>
#include <dlfcn.h>

void foo(void)
{
    printf("foo\n");
}

void bar(void)
{
    printf("bar\n");
}


void baz(void)
{
    printf("baz\n");
}

int main(int argc, char *argv[])
{
    void *hndl = dlopen(NULL, RTLD_LAZY);
    void (*fn)(void) = dlsym(hndl, argv[1]);

    if (fn) {
        fn();
    } else {
        printf("Function %s() not found\n", argv[1]);
    }

    return 0;
}
#include <stdio.h>
#include <stdlib.h>

const char file_includes[] = "#include <stdio.h>\n";
const char file_functions[] = 
  "void print() {\n"
  "    printf(\"print function called\\n\");\n"
  "}\n"
  "void print2() {\n"
  "    printf(\"second print function called\\n\");\n"
  "}\n";

const char file_main_prolog[] = "int main() {\n";
const char file_main_epilog[] = "  return 0;\n}\n";


int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("invalid program arguments\n");
    return 1;
  }

  FILE *fout = fopen("function_call.c", "w");

  fprintf(fout, "%s", file_includes);
  fprintf(fout, "%s", file_functions);
  fprintf(fout, "%s", file_main_prolog);
  fprintf(fout, "  %s();\n", argv[1]);
  fprintf(fout, "%s", file_main_epilog);

  fclose(fout);

  system("gcc -o function_call function_call.c");
  system("./function_call");

  return 0;
}