不使用/ NODEFAULTLIB调用构造函数

时间:2019-05-24 10:01:53

标签: c++ constructor llvm

我正在使用/ NODEFAULTLIB禁用CRT(C运行时),但是未调用我的构造函数,这最终在std :: map(访问冲突)中导致错误,因为自std ::起未正确初始化地图构造函数没有被调用。

使用LLVM 8.0.0编译的代码,以调试模式x86编译

class c_test
{
public:

    c_test( int a ) // Constructor not called
    {
        printf( "Test: %i\n", a ); // Doesn't appear and breakpoint is not reached
    }

    void add( const std::string& key, const std::string& val )
    {
        _data[ key ] = val;
    }

private:
    std::map< std::string, std::string >                        _data;
};

c_test test{ 1337 };

int main()
{
    test.add( "qwrqrqr", "23142421" );
    test.add( "awrqw", "12asa1faf" );

    return 1;
}

我已经实现了自己的函数new(HeapAlloc),delete(HeapFree),printf,memcpy,memmove等,并且都运行良好,我不知道为什么会这样。

2 个答案:

答案 0 :(得分:1)

禁用CRT是疯狂的事情。

这执行关键功能,例如静态初始化。缺少静态初始化是地图处于残缺状态的原因。我也完全希望标准库的各个部分都停止工作。您确实为自己制造了一个大问题。

不要重新发明一些关键的机械设备-重新打开CRT并使用专家编写的代码。将其关闭实际上并没有获得相对价值。

答案 1 :(得分:0)

我发现了问题并解决了,另一个论坛的一个人说,我需要手动调用存储在.CRT节中的指针中的构造函数,我做到了,而且效果很好

enter image description here

enter image description here

enter image description here

我刚刚调用了_GLOBAL__sub_I_main_cpp函数,该函数调用了我的构造函数并解决了所有问题,感谢您的回答。