C++ 在 main 之前调用一个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10897552/
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 14:35:49  来源:igfitidea点击:

Call a function before main

c++cstartupmain

提问by Nick

Possible Duplicate:
Is main() really start of a C++ program?

可能的重复:
main() 真的是 C++ 程序的开始吗?

Is possible to call my function before program's startup? How can i do this work in C++or C?

可以在程序启动之前调用我的函数吗?我怎样才能在C++或 中完成这项工作C

回答by Luchian Grigore

You can have a global variable or a staticclass member.

您可以拥有全局变量或static类成员。

1) staticclass member

1)static班级成员

//BeforeMain.h
class BeforeMain
{
    static bool foo;
};

//BeforeMain.cpp
#include "BeforeMain.h"
bool BeforeMain::foo = foo();

2) global variable

2) 全局变量

bool b = foo();
int main()
{
}

Note this link - Mirror of http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14/ proposed alternative- posted by Lundin.

请注意此链接 - http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14 的镜像/提议的替代方案- 由 Lundin 发布。

回答by gliderkite

In C++there is a simple method: use the constructor of a global object.

C++有一个简单的方法:用一个全局对象的构造函数

class StartUp
{
public:
   StartUp()
   { foo(); }
};

StartUp startup; // A global instance

int main()
{
    ...
}

This because the global object is constructed before main()starts. As Lundinpointed out, pay attention to the static initialization order fiasco.

这是因为全局对象是在main()开始之前构造的。正如Lundin 所指出的,注意静态初始化顺序 fiasco

回答by Eight

If using gccand g++compilers then this can be done by using __attribute__((constructor))

如果使用gccg++编译器,那么这可以通过使用来完成__attribute__((constructor))

eg::
In gcc (c) ::

例如::
在gcc(c)中::

#include <stdio.h>

void beforeMain (void) __attribute__((constructor));

void beforeMain (void)
{
  printf ("\nbefore main\n");
}

int main ()
{
 printf ("\ninside main \n");
 return 0;
}

In g++ (c++) ::

在 g++ (c++) ::

#include <iostream>
using namespace std;
void beforeMain (void) __attribute__((constructor));

void beforeMain (void)
{
  cout<<"\nbefore main\n";
}

int main ()
{
  cout<<"\ninside main \n";
  return 0;
}

回答by CB Bailey

In C++ it is possible, e.g.

在 C++ 中是可能的,例如

static int dummy = (some_function(), 0);

int main() {}

In C this is not allowed because initializers for objects with static storage duration must be constant expressions.

在 C 中这是不允许的,因为具有静态存储持续时间的对象的初始值设定项必须是常量表达式。

回答by kapilddit

I would suggest you to refer this link..

我建议你参考这个链接..

http://bhushanverma.blogspot.in/2010/09/how-to-call-function-before-main-and.html

http://bhushanverma.blogspot.in/2010/09/how-to-call-function-before-main-and.html

For GCC compiler on Linux/Solaris:

对于 Linux/Solaris 上的 GCC 编译器:

#include

void my_ctor (void) __attribute__ ((constructor));
void my_dtor (void) __attribute__ ((destructor));

void
my_ctor (void)
{
printf ("hello before main()\n");
}

void
my_dtor (void)
{
printf ("bye after main()\n");
}

int
main (void)
{
printf ("hello\n");
return 0;
}

$gcc main.c
$./a.out
hello before main()
hello
bye after main()