GCC和MSVC的INITIALIZER的实现
生活随笔
收集整理的這篇文章主要介紹了
GCC和MSVC的INITIALIZER的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
熟悉linux內核編程的應該知道內核模塊有一個宏叫 module_init,當內核模塊被靜態編譯到內核后,會在內核init階段調用每個被module_init聲明過的函數。這是如何實現的呢?其實是用到了鏈接器的特性。具體可參考
https://blog.csdn.net/lu_embedded/article/details/51432616
大致就是告訴連接器將函數指針放到一個特定的程序段,然后在需要的時候遍歷這個程序段,拿到每個函數指針,然后調用。
那么MSVC有沒有這樣的特性呢,搜索了一番,答案是有的,具體可參考
https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-initialization?redirectedfrom=MSDN&view=msvc-160
原理基本上是一樣的,只不過是調用時機是VC runtime。
網上還找到了利用gcc runtime 和 vc runtime實現的跨平臺INITIALIZER
// Initializer/finalizer sample for MSVC and GCC/Clang.// 2010-2016 Joe Lowe. Released into the public domain. #include <stdio.h> #include <stdlib.h>#ifdef __cplusplus#define INITIALIZER(f) \static void f(void); \struct f##_t_ { f##_t_(void) { f(); } }; static f##_t_ f##_; \static void f(void) #elif defined(_MSC_VER)#pragma section(".CRT$XCU",read)#define INITIALIZER2_(f,p) \static void f(void); \__declspec(allocate(".CRT$XCU")) void (*f##_)(void) = f; \__pragma(comment(linker,"/include:" p #f "_")) \static void f(void)#ifdef _WIN64#define INITIALIZER(f) INITIALIZER2_(f,"")#else#define INITIALIZER(f) INITIALIZER2_(f,"_")#endif #else#define INITIALIZER(f) \static void f(void) __attribute__((constructor)); \static void f(void) #endifstatic void finalize(void) {printf( "finalize\n"); }INITIALIZER( initialize) {printf( "initialize\n");atexit( finalize); }int main( int argc, char** argv) {printf( "main\n");return 0; }另外,我也實現了一個簡單的MSVC版本,如下。
#include <windows.h>//以下內容可放到公共頭文件 #pragma section(".MYINIT$A", read) #pragma section(".MYINIT$P", read) #pragma section(".MYINIT$Z", read) typedef void (*PFN_INIT)(void);#define _INIT_FN(fn, pre) \static void fn(void); \__declspec(allocate(".MYINIT$P")) PFN_INIT fn##_ = fn; \__pragma(comment(linker,"/include:" pre #fn "_")) \static void fn(void) #ifdef _WIN64 #define INIT_FN(fn) _INIT_FN(fn, "") #else #define INIT_FN(fn) _INIT_FN(fn, "_") #endif//以下內容放到 main 文件 __declspec(allocate(".MYINIT$A")) int __myinit_a = 0; __declspec(allocate(".MYINIT$Z")) int __myinit_z = 0;void CallInitFn(void) {PFN_INIT *start = (PFN_INIT *)&__myinit_a;PFN_INIT *end = (PFN_INIT *)&__myinit_z;while(start < end){if(*start){(*start)();}start++;} }int main(int argc, char *argv[]) {CallInitFn();Sleep(1000); }//以下INIT函數可分散放到不同的源文件 INIT_FN(init3) {OutputDebugStringA("init3\n"); }INIT_FN(init4) {OutputDebugStringA("init4\n"); }?
總結
以上是生活随笔為你收集整理的GCC和MSVC的INITIALIZER的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 飞利浦AC6608空气净化器粉尘传感器维
- 下一篇: 精简版sprintf适合嵌入式使用