gpt4 book ai didi

c++ - 如何显式调用CRT启动函数

转载 作者:行者123 更新时间:2023-11-28 04:59:53 27 4
gpt4 key购买 nike

有什么方法可以在我的代码中显式调用通常在 mainCRTStartup 函数中调用的函数?我对 initterminitterm_e 函数特别感兴趣。甚至认为我的程序与 crt 链接(它必须是,对吗?)我不能只调用 initterm - 我试图声明并只是调用,但我得到了 undefined reference 。

有什么办法吗?

最佳答案

_initterm , 和 _initterm_e (注意在 msdn 中 _initterm_e 声明错误 - 实际上它使用了 _PIFV 类型)由不同的 CRT dll 导出。它也在 lib 文件中声明。这个功能的实现非常简单:

typedef void (__cdecl *_PVFV)();

void _initterm(const _PVFV *ppfn, const _PVFV *end)
{
do
{
if (_PVFV pfn = *++ppfn)
{
pfn();
}
} while (ppfn < end);
}

typedef int (__cdecl *_PIFV)();

int _initterm_e(const _PIFV *ppfn, const _PIFV *end)
{
do
{
if (_PIFV pfn = *++ppfn)
{
if (int err = pfn()) return err;
}
} while (ppfn < end);

return 0;
}

因此您可以导入它或使用自己的实现。这里更快的问题 - 什么用作输入参数?

c++(CL 编译器的最小值)使用 ( ".CRT$XCA", ".CRT$XCZ") 部分放置 c++ 全局初始化。所以我们接下来可以做:

extern "C"
{
#pragma const_seg(".CRT$XCA")
const _PVFV __xc_a = 0;
#pragma const_seg(".CRT$XCZ")
const _PVFV __xc_z = 0;

#pragma const_seg(".CRT$XIA")
const _PVFV __xi_a = 0;
#pragma const_seg(".CRT$XIZ")
const _PVFV __xi_z = 0;
}

/*
* do initializations
*/
initret = _initterm_e( &__xi_a, &__xi_z );
if ( initret != 0 )
return initret;

/*
* do C++ initializations
*/
_initterm( &__xc_a, &__xc_z );

但这里需要很好的理解——我们做什么以及为什么做

关于c++ - 如何显式调用CRT启动函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46331874/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com