如何在VC进入主()程序之前执行一些code? [英] How to execute some code before entering the main() routine in VC?
问题描述
我在读微软的CRT源$ C $ C,我可以拿出下面code,其中的功能__initstdio1将main()例程之前执行。
现在的问题是,如何在VC(而不是VC ++ code)?
进入主()程序之前执行一些code 的#include<&stdio.h中GT;的#pragma节(CRT $ XIC,长,读取)INT __cdecl __initstdio1(无效);#定义_CRTALLOC(X)__declspec(分配(X))_CRTALLOC(CRT $ XIC。),静态PINIT = __initstdio1;INT Z = 1;INT __cdecl __initstdio1(无效){
Z = 10;
返回0;
}诠释主要(无效){
的printf(!之前的主有些code \\ n);
的printf(Z =%d个\\ N,Z);
的printf(结束\\ n!);
返回0;
}
的输出将是:
有些$ C $之前主要的C!
Z = 10
结束!
不过,我无法理解的code。
我已经做了.CRT $西昌一些谷歌,但没有运气找到。可以在上面code段部分专家给我解释一下,尤其是以下几个方面:
- 这是什么行
_CRTALLOC(CRT $ XIC。),静态PINIT = __initstdio1;
是什么意思?什么是变量PINIT的意义是什么? - 在编译过程中,编译器(cl.exe时)抛出一个警告,如下图所示:
微软(R)32位C / C ++优化编译器版本为15.00.30729.01的80x86
版权所有(C)微软公司。保留所有权利。
stdmacro.c
stdmacro.c(9):警告C4047:初始化':'诠释',从'廉政间接水平(不同__
CDECL *)(无效)'
微软(R)增量链接器版本9.00.30729.01
版权所有(C)微软公司。版权所有。/out:stdmacro.exe
stdmacro.obj
什么是纠正行动需要做删除的警告信息?
先谢谢了。
补充:
我修改了code和给类型为PINIT _PIFV。现在,警告消息已经一去不复返了。
新的code是如下:
的#include<&stdio.h中GT;的#pragma节(CRT $ XIC1,长,读取)INT __cdecl __initstdio1(无效);的typedef INT(__cdecl * _PIFV)(无效);#定义_CRTALLOC(X)__declspec(分配(X))_CRTALLOC(CRT $ XIC1)静态_PIFV pinit1 = __initstdio1;INT Z = 1;INT __cdecl __initstdio1(无效){
Z = 100; 返回0;
}诠释主要(无效){
的printf(!之前的主有些code \\ n);
的printf(Z =%d个\\ N,Z);
的printf(结束\\ n!);
返回0;
}
有一些信息 rel=\"nofollow\"> (搜索CRT)。变量 PINIT
的意义是没有的,它只是一块放置在可执行文件,在运行时可以找到它的数据。不过,我劝你给它一个类型,像这样的:
_CRTALLOC(CRT $ XIC),静态无效(* PINIT)()= ...
链接器警告可能只是警告你,你有一个具有 INT
返回类型,但也没有返回值的函数(可能是您最好改变返回类型无效
)。
I am reading Microsoft's CRT source code, and I can come up with the following code, where the function __initstdio1 will be executed before main() routine.
The question is, how to execute some code before entering the main() routine in VC (not VC++ code)?
#include <stdio.h>
#pragma section(".CRT$XIC",long,read)
int __cdecl __initstdio1(void);
#define _CRTALLOC(x) __declspec(allocate(x))
_CRTALLOC(".CRT$XIC") static pinit = __initstdio1;
int z = 1;
int __cdecl __initstdio1(void) {
z = 10;
return 0;
}
int main(void) {
printf("Some code before main!\n");
printf("z = %d\n", z);
printf("End!\n");
return 0;
}
The output will be:
Some code before main!
z = 10
End!
However, I am not able to understand the code.
I have done some google on .CRT$XIC but no luck is found. Can some expert explain above code segment to me, especially the followings:
- What does this line
_CRTALLOC(".CRT$XIC") static pinit = __initstdio1;
mean? What is the significance of the variable pinit? - During compilation the compiler (cl.exe) throws a warning saying as below:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved.
stdmacro.c
stdmacro.c(9) : warning C4047: 'initializing' : 'int' differs in levels of indirection from 'int (__
cdecl *)(void)'
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:stdmacro.exe
stdmacro.obj
What is the corrective action needs to be done to remove the warning message?
Thanks in advance.
Added:
I have modified the code and give type to pinit as _PIFV. Now the warning message is gone.
The new code is as follows:
#include <stdio.h>
#pragma section(".CRT$XIC1",long,read)
int __cdecl __initstdio1(void);
typedef int (__cdecl *_PIFV)(void);
#define _CRTALLOC(x) __declspec(allocate(x))
_CRTALLOC(".CRT$XIC1") static _PIFV pinit1 = __initstdio1;
int z = 1;
int __cdecl __initstdio1(void) {
z = 100;
return 0;
}
int main(void) {
printf("Some code before main!\n");
printf("z = %d\n", z);
printf("End!\n");
return 0;
}
There's some information here (search for CRT). The significance of variable pinit
is none, it's just a piece of data placed in the executable, where the runtime can find it. However, I would advise you to give it a type, like this:
_CRTALLOC(".CRT$XIC") static void (*pinit)()=...
The linker warning probably just warns you you have a function that has int
return type, but doesn't return anything (probably you'd better change the return type to void
).
这篇关于如何在VC进入主()程序之前执行一些code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!