qemu-vexpress-a9 上c++ 全局类的构造函数没有调用

发布于 2021-04-30 13:32:58

win10 qemu-vexpress-a9 环境下面,局部变量的c++能正使用,但全局变量的那些类,构造函数没有调用。cplusplus_system_init函数有调用,__ctors_start__和__ctors_end__地址一样。

RT_WEAK int cplusplus_system_init(void)
{
#if defined(__CC_ARM) || defined(__CLANG_ARM)
    /* If there is no SHT$$INIT_ARRAY, calling
     * $Super$$__cpp_initialize__aeabi_() will cause fault. At least until Keil5.12
     * the problem still exists. So we have to initialize the C++ runtime by ourself.
     */
    typedef void PROC();
    extern const unsigned long SHT$$INIT_ARRAY$$Base[];
    extern const unsigned long SHT$$INIT_ARRAY$$Limit[];

    const unsigned long *base = SHT$$INIT_ARRAY$$Base;
    const unsigned long *lim  = SHT$$INIT_ARRAY$$Limit;

    for (; base != lim; base++)
    {
        PROC *proc = (PROC *)((const char *)base + *base);
        (*proc)();
    }
#elif defined(__GNUC__)
    typedef void(*pfunc)();
    extern pfunc __ctors_start__[];
    extern pfunc __ctors_end__[];
    pfunc *p;
    for (p = __ctors_start__; p < __ctors_end__; p++)
        (*p)();
#endif

    return 0;
}

main.c

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "test.h"
int main(void)
{
    printf("test: %d\n",test_b());
    return 0;
}

test.h

int test_b(void);

test.cpp

#include <stdio.h>
class B{
    public:
    B(){
        printf("B::B()\n");
        b = 5;
    }
    ~B(){}
    int b;
};
B b;
extern "C" int test_b(void){
    return b.b;
}

结果

PS H:\work\qemu\rt-thread\rt-thread\bsp\qemu-vexpress-a9> .\qemu.bat
WARNING: Image format was not specified for 'sd.bin' and probing guessed raw.
         Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
         Specify the 'raw' format explicitly to remove the restrictions.
dsound: Could not initialize DirectSoundCapture
dsound: Reason: No sound driver is available for use, or the given GUID is not a valid DirectSound device ID

 \ | /
- RT -     Thread Operating System
 / | \     4.0.3 build Apr 29 2021
 2006 - 2021 Copyright by rt-thread team
lwIP-2.0.2 initialized!
[32m[I/sal.skt] Socket Abstraction Layer initialize success.
m[I/SDIO] SD card capacity 65536 KB.
[I/SDIO] switching card to hig     h speed failed!
file system initialization done!
test: 0
msh />

查看更多

关注者
0
被浏览
55
2 个回答
flashman2002
flashman2002 2021-04-30
cxhxy12345
cxhxy12345 2021-04-30

你这里是不是因该先声明后赋值,
class B{

public:
B(){
    printf("B::B()\n");
     int b = 5;//加个声明
}
~B(){}
int b;//这里去掉

};

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览