简体   繁体   English

通过函数指针定义“main”

[英]Defining “main” by a function pointer

In C++, is it possible for main to be defined by a function pointer? 在C ++中,main是否可以通过函数指针定义? For example: 例如:

int f(int, char**) {
    return 0;
}

int (*main)(int, char**) = &f;

This code compiles and links correctly, but triggers a segmentation fault when it's run. 此代码正确编译和链接,但在运行时会触发分段错误。 I believe that may be because it's trying to execute the value of the function pointer as code. 我相信这可能是因为它试图将函数指针的值作为代码执行。

Additionally, if it's not possible in plain C++, then can it be achieved by non-standard features of gcc (maybe somehow changing the type of the exported symbol). 另外,如果在普通的C ++中不可能,那么可以通过gcc的非标准功能来实现(可能以某种方式改变导出符号的类型)。

Finally, if it can't be achieved with gcc directives, can it be done with a custom linker script? 最后,如果使用gcc指令无法实现,可以使用自定义链接描述文件吗?

Paragraph 3.6.1/1 of the C++ Standard says: C ++标准的第3.6.1 / 1段说:

"A program shall contain a global function called main, which is the designated start of the program" “程序应包含一个名为main的全局函数 ,它是程序的指定开始”

This makes the pointer declaration illegal. 这使得指针声明非法。

I believe what you try to do is not possible in c++. 我相信你尝试做的事情在c ++中是不可能的。 Easiest solution I can think of is to simply call f in your main: 我能想到的最简单的解决方案就是在你的主要部分简单地调用f

int f(int, char**) {
    return 0;
}

int main(int argc, char** argv) {
  return f(argc, argv);
}

I also can't imagine a scenario where the above solution will not do. 我也无法想象上述解决方案不会做的情况。

I can't imagine why you would want this, but no, it's definitely not possible with plain C++. 我无法想象为什么你会想要这个,但不,这绝对不可能用普通的C ++。

If you really need something like this, just use a function pointer that you call as the first command in main. 如果你真的需要这样的东西,只需使用你在main中调用的第一个命令的函数指针。

With GCC you can pass options to the linker with the -Wl option. 使用GCC,您可以使用-Wl选项将选项传递给链接器。 Then you can use the --entry option of the linker to specify which function should be called first. 然后,您可以使用链接器的--entry选项指定应首先调用哪个函数。

WARNING: This will not do what you expect! 警告:符合您的期望! The entry function of a program is actually not the main function, but another function which sets up the run-time environment and then call your main function. 程序的入口函数实际上不是 main函数,而是另一个设置运行时环境然后调用main函数的函数。

If you are on linux, & using gcc, you can declare main as an alias to any other function. 如果您使用的是linux,并使用gcc,则可以将main声明为任何其他函数的alias

eg, below code works (with gcc at least, not tested with g++): 例如,下面的代码工作(至少使用gcc,不使用g ++测试):

int f(int argc, char* argv[]) {
    printf("hello world\n");
    return 0;
}

int main(int argc, char* argv[]) __attribute__ ((alias("f")));

main is a keyword in C as well as in C++ and gets overwritten as the function pointer name in your program. main 是C语言和C ++中的关键字, 并被覆盖为程序中的函数指针名称。 main defines the entry point of execution. main定义了执行的入口点。 Hence your program does not have that function and overwrites main it segfaults. 因此,您的程序没有该功能,并覆盖了main错误。

Another "dirty" way is to create a fake function using assembly & as the first instruction of that function, jump to f . 另一种“脏”的方法是使用程序集创建一个假函数并作为该函数的第一条指令,跳转到f

Below is a gcc specific code for intel target: 以下是针对intel目标的gcc特定代码:

asm(".globl main");           // main is not static
asm(".type main, @function"); // main is a 'function'
asm("main: jmp f");           // main starts here. jump to `f`

int g(){ //Some random function may be present in between "main" & "f"...
    puts("hello universe");
}

int f(int argc, char* argv[]) {
    int i;
    puts("hello world");
    printf("%d arguments given\n",argc - 1);
    for(i=0;i<argc;i++) puts (argv[i]);
    g();
    return 0;
}

int (*main)(int, char**) = &f; int(* main)(int,char **)=&f;

doesn't look correct. 看起来不正确。

What about 关于什么

int (*main)(int, char**) = f;

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM