在C++工程中main函数之前跑代码的廉价方法
2015-11-10 00:17
357 查看
// test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <windows.h> #include <crtdbg.h> /// 在C++工程中main函数之前跑代码的廉价方法 /// 利用全局变量可以赋可变初值的事实 /// mainCRTStartup() => _cinit() => 全局变量(静态, 普通)赋初值 extern "C" int foo(void); extern "C" int foo1(void); /// 执行顺序和全局变量声明顺序相同 /// 先执行foo1, 再执行foo(); static int gs_iTest = foo1(); int g_iTest = foo(); int main(int argc, char* argv[]) { printf("&main = %p\n", &main); getchar(); return 0; } extern "C" int foo(void) { /// 执行不带CRT函数的代码 MessageBox(NULL, "foo before main", "test", MB_OK); return 0; } extern "C" int foo1(void) { /// 执行不带CRT函数的代码 MessageBox(NULL, "foo1 before main", "test", MB_OK); return 0; }
效果
相关文章推荐
- C++在非面向对象方面的一些特性(基本)
- 排序算法(七):归并排序
- 求平均数函数
- 链表的建立以及增加、删除结点的C语言完整代码
- C++抛出异常时无法捕捉到异常,致使程序崩溃
- C/C++中extern关键字详解
- C++11新特性之 Move semantics(移动语义)
- C++11新特性之 Move semantics(移动语义)
- C++ - 异常处理:try,catch,throw,finally的用法
- <转载与补充>C与C++动态内存分配
- C语言指针学习
- [挖坑]const限定符
- C++ erase的用法
- 数组作为函数形参的一点总结
- effective c++尽可能延后变量定义式的出现时间
- [C++渐进]引用与指针
- C/Cplusplus
- 什么是bind函数,为什么要使用bind函数
- 全错位排列问题
- lambda表达式的实质——什么是lambda表达式,为什么要使用lambda表达式