之前在C主程序调用一些功能 [英] Calling some functions before main in C

查看:164
本文介绍了之前在C主程序调用一些功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想之前功能做一些东西。我有多个源文件。在每个文件中,有一些需要工作之前来完成。这是在C ++中没有问题,但用C有问题的。

I'd like to do some stuffs before main function. I have multiple source files. In each file, there is some work that needs to be done before main. It was no problem in C++, but problematic with C.

在C ++中,这可以通过两种方式来完成:

In C++, this can be done by two ways:


  1. 开拓一个全球性的类/结构的构造。

  2. 调用一个函数的全局变量

例如,静态const int的__register_dummy_ = __AddRegisterMetaInfo(...);

不过,在C,无论方式是不可能的。很显然,没有构造函数。所以,第一个选项是天生不可能的。

However, in C, either ways is impossible. Obviously, there is no constructor. So, the first option is inherently impossible.

我认为第二个方案是可能的,但在C不编译(我只用Visual C ++进行测试,它给人的 C2099 )。仅C允许一个常数到非自动变量。

I thought that the second option would be possible, but not compiled in C (I tested only with Visual C++. It gives C2099.). C only allows a constant to a non-automatic variable.

有什么办法之前主要调用某些功能?

Is there any way to call some functions before main?

修改:似乎很多人只是误解了我真正想做的事。对不起,在一种简化的方式写了这个问题。

EDIT: It seems that many people just got misunderstand what I really wanted to do. Sorry for writing this question in a simplified way.

我需要做的是落实了几分C ++运行时类信息的功能,就像的 MFC的做法。在这种方法中,我需要从所有源$ C ​​$ C的一些信息。例如,假设每个源文件有一个类的定义,我想看到​​的所有信息(例如,类名和父类)。最简单的方法是将每个文件静态构造函数,每个构造函数访问全局数据结构,并登记其信息。但是,我也想找到一种方法来实施 pre_main_job 类似的事情C $ C>不能回答我。

What I needed to do is implementing a sort of C++ runtime class information feature, just like MFC's approach. In this approach, I need to get some information from all source code. For example, say each source file has a definition of a class, and I'd like to see all information (e.g., class names and the parent class). The easiest way is placing a static constructor in each file, and each constructor accesses a global data structure and register its information. But, I also wanted to find a way to implement a similar thing in C. So, simply calling a pre_main_job in main can't be an answer to me.

请注意,这种滥用静态构造函数中还可以在LLVM编译器套件被发现。每个优化/分析功能实现为一通。所有这些通行证通过一个静态构造函数注册。

Note that this abuse of static constructor also can be found in LLVM compiler suite. Each optimization/analysis feature is implemented as a pass. All these passes are registered via a static constructor.

推荐答案

对于一些编译器特定解决方案,你可以看看从OpenSSL的分布fips_ premain.c文件(可在网上查看一堆的地方,<一个href=\"http://www.opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_$p$pmain.c\">here例如)。

For a number of compiler specific solutions you can take a look at the fips_premain.c file from the OpenSSL distribution (you can view it online in a bunch of places, here for instance).

MSVC的特定部分看起来像(前执行 FINGERPRINT_ $ P $段Pmain 是函数main ):

The MSVC specific part looks something like (FINGERPRINT_premain being the function to be executed before main):

# ifdef _WINDLL
  __declspec(dllexport) /* this is essentially cosmetics... */
# endif
  void FINGERPRINT_premain(void);
  static int premain_wrapper(void) { FINGERPRINT_premain(); return 0; }
# ifdef _WIN64
# pragma section(".CRT$XCU",read)
  __declspec(allocate(".CRT$XCU"))
# else
# pragma data_seg(".CRT$XCU")
# endif
  static int (*p)(void) = premain_wrapper;
  /* This results in pointer to premain to appear in .CRT segment,
   * which is traversed by Visual C run-time initialization code.
   * This applies to both Win32 and [all flavors of] Win64. */
# pragma data_seg()

这篇关于之前在C主程序调用一些功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆